week2practice
user_1671475754
javascript
4 years ago
5.1 kB
21
Indexable
//*************************PROBLEM 1************************* // plan: //set counter 0 //get key value from obj //check if KEY includes cute or adorable, then counter++ //check if VALUE includes cute or adorable, then counter++ //return counter const miahsCats = (obj)=>{ let counter = 0; for([key,value] of Object.entries(obj)){ let current_key = key.toLowerCase(); let current_value = value.toLowerCase(); if(current_key.includes('cute') || current_key.includes('adorable')){ counter ++; } if(current_value.includes('cute') || current_value.includes('adorable')){ counter ++; } } return counter; } //*************************PROBLEM 2************************* // plan: do 2 splits and check w/ includes // split_by_comma // iterate split_by_comma & split each ele by semi-colon const spiderManWriters = (string) =>{ // split_by_comma = ['Angela T: wrote about Spiderman', 'Miah B: no Spiderman'] const split_by_comma = string.split(', '); // split_by_semi = [['Angela T', 'wrote about Spiderman'], ...] const split_by_semi = split_by_comma.map((kv_string)=>kv_string.split(': ')) // compare w/ includes for each author and add to obj. const who_didnt_write_about_spidy = {}; split_by_semi.forEach(ele=>{ //ele = ['Angela T', 'wrote about Spiderman'] if(ele[1] !== undefined && ele[1].includes('no Spiderman')){ // console.log(ele[0], ele[1]) who_didnt_write_about_spidy[ele[0]] = ele[1]; } }) return who_didnt_write_about_spidy; } //*************************PROBLEM 3************************* // plan: // get percentage to pay after discount // iterate through wishlist // for each obj, we get discounted price (percent to pay * price) // add discounted price to counter const applyCoupon = (discount, wishList) =>{ const percent_to_pay = (100 - discount) / 100; // (100 - 10) / 100 = 0.9 let total_price_after_discount = 0; wishList.forEach(ele => { //ele = {name: "Xbox", price: 80}, const ele_price = ele["price"] const discounted_price = percent_to_pay * ele_price; // 72 = 0.9 * 80 total_price_after_discount += discounted_price; }) // console.log(total_price_after_discount); return total_price_after_discount; } //*************************PROBLEM 4************************* // plan: // declare total // outer func returns declare inner function // inner func sets total to dice * roll (closure = access to an outer func’s scope from inner) // inner func returns total //using arrow function expression const dynamicDice = dice =>{ let total; return roll => { total = dice * roll; return total; } } //using vanilla function declaration function dynamicDice2(dice){ let total; return function(roll){ //inner function creates a CLOSURE over 'total' variable total = dice * roll; return total; } } //*************************PROBLEM 5************************* //note: *given at least one arg //plan: //function parameter takes in arguments w/ rest (...nums) --> nums is now an array of numbers //iterate through 'nums' and sum up total // ------------------vanilla for loop------------------- // const manyArgs = (...args) => { // let total = args[0]; // for(let i=1; i<args.length; i++){ // total *= args[i]; // } // return total; // } // ------------------with map------------------- // const manyArgs = (...args) => { // let total = 1; // //here we only use array map method as an iterator and multiplying num to total // //NOTE: This is INEFFICIENT for memory as we do not need the array provided by map // const unused_array_from_map = args.map(num => total*=num) //arrow func with IMPLICIT return logic // // console.log(unused_array_from_map) //see what this prints // return total; // } // manyArgs(1, 2, 3, 4, 5, 6, 7) // 5040 // manyArgs(1, 2, 5, 6, 7) // 420 // ------------------with forEach------------------- // const manyArgs = (...args) => { // let total = 1; // //forEach does not return an array, just iterates and allow us access of ele in args. // args.forEach( (ele) => total*=ele) // return total; // } // ------------------with reduce------------------- const manyArgs = (...args) => { // DECLARATION of callback to be passed to reduce. total = accum const multiply_accum_callback = (total, num) =>{ let new_total = total*num return new_total //explicit return of the new 'accum' value to be used by reduce. } const default_total_value = 1; // here, reduce takes in a callback function and a default value. // callback returns: new accum value after applying logic that will be tracked let final_total = args.reduce(multiply_accum_callback, default_total_value) //if no default provided, reduce will make default accum the first ele in the array (args). return final_total; }
Editor is loading...