From cfd7f79b423d676238127a20c4563dce537e6757 Mon Sep 17 00:00:00 2001 From: Tylar Washington-Beechum Date: Mon, 27 Apr 2020 00:55:46 -0400 Subject: [PATCH 1/2] Just saving 4/27/2020 --- index.js | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index f61169392..4763f38fc 100644 --- a/index.js +++ b/index.js @@ -48,8 +48,8 @@ function processFirstItem(stringList, callback) { * [2] Invoking `processLength` passing `[]` and `(num) => "There are " + num`, * should return "There are 0". */ -function processLength(/* CODE HERE */) { - /* CODE HERE */ +function processLength(list,callback) { + return callback(list.length); } /** @@ -66,8 +66,16 @@ function processLength(/* CODE HERE */) { * Invoking `processLastItem` passing `['foo', 'bar']` and `(str) => str + str`, * should return 'barbar'. */ -function processLastItem(/* CODE HERE */) { - /* CODE HERE */ +function processLastItem(stringList,callback) { + let Lastitem = []; + for(let i = 0; i < stringList.length;i++){ + if(stringList[i] === 'barbar'){ + Lastitem.push(true) + } else { + Lastitem.push(false) + } + } + return callback(stringList); } /** @@ -88,8 +96,8 @@ function processLastItem(/* CODE HERE */) { * [2] Invoking `processSum` passing `-5`, '-1', and `(num) => num + 1000`, * should return 994. */ -function processSum(/* CODE HERE */) { - /* CODE HERE */ +function processSum(num1,num2,callback) { + return callback(num1 + num2); } /** @@ -110,8 +118,8 @@ function processSum(/* CODE HERE */) { * [2] Invoking `processProduct` passing 25 and 0 and `(num) => num + 1000`, * should return 1000. */ -function processProduct(/* CODE HERE */) { - /* CODE HERE */ +function processProduct(num1,num2,callback) { + return callback(num1,num2) } /** @@ -132,13 +140,13 @@ function processProduct(/* CODE HERE */) { * [2] Invoking `processDuplicateFree` passing `[1,1,2,2,3]` and `(arr) => arr.length`, * should return 3. */ -function processDuplicateFree(/* CODE HERE ONLY AFTER COMPLETING ALL OTHER TASKS */) { +function processDuplicateFree(list,callback) { /* CODE HERE ONLY AFTER COMPLETING ALL OTHER TASKS */ } /////////////// HIGHER-ORDER ARRAY METHODS /////////////// /////////////// HIGHER-ORDER ARRAY METHODS /////////////// - + /** * ### Challenge `lowerCaseStrings` @@ -155,8 +163,12 @@ function processDuplicateFree(/* CODE HERE ONLY AFTER COMPLETING ALL OTHER TASKS * * [2] Invoking `lowerCaseStrings` with `['a', 'b', 'c' ]` will return `[ 'a', 'b', 'c' ]`. */ -function lowerCaseStrings(/* code here */) { - /* code here */ +function lowerCaseStrings(strings) { + let lowercase = []; + for(let i = 0;i < strings.length;i++){ + lowercase.push(strings[i].toLowerCase()); + } + return lowercase; } /** From a3dfe7eea2b57f9696da3f05aa7372537dc8c87e Mon Sep 17 00:00:00 2001 From: Tylar Washington-Beechum Date: Thu, 30 Apr 2020 20:30:09 -0400 Subject: [PATCH 2/2] Save 4/30/2020 --- index.js | 63 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 4763f38fc..76ce27a61 100644 --- a/index.js +++ b/index.js @@ -67,15 +67,7 @@ function processLength(list,callback) { * should return 'barbar'. */ function processLastItem(stringList,callback) { - let Lastitem = []; - for(let i = 0; i < stringList.length;i++){ - if(stringList[i] === 'barbar'){ - Lastitem.push(true) - } else { - Lastitem.push(false) - } - } - return callback(stringList); + return callback(stringList.slice(-1)[0]); } /** @@ -119,7 +111,7 @@ function processSum(num1,num2,callback) { * should return 1000. */ function processProduct(num1,num2,callback) { - return callback(num1,num2) + return callback(num1 * num2); } /** @@ -186,8 +178,17 @@ function lowerCaseStrings(strings) { * * [2] Invoking `isItAnApple` with `['a', 'b', 'c' ]` will return `[ false, false, false ]`. */ -function isItAnApple(/* code here */) { - /* code here */ +function isItAnApple(strings) { + let AnApple = []; + + for(let i = 0;i< strings.length;i++){ + if(strings[i] === 'apple'){ + AnApple.push(true) + } else{ + AnApple.push(false); + } + } + return AnApple; } /** @@ -206,8 +207,14 @@ function isItAnApple(/* code here */) { * * [2] Invoking `removeApple` with `['a', 'b', 'c' ]` will return `[ 'a', 'b', 'c' ]`. */ -function removeApple(/* code here */) { - /* code here */ +function removeApple(strings) { + let goneapple = []; + strings.filter(function(apple) { + if (apple !=='apple'){ + goneapple.push(apple); + } + }); + return goneapple; } /** @@ -225,8 +232,12 @@ function removeApple(/* code here */) { * * [2] Invoking `stringSmash` with `['a', 'b', 'c' ]` will return `abc`. */ -function stringSmash(/* code here */) { - /* code here */ +function stringSmash(strings){ + let reducer = (accumulator, currentvalue) => accumulator + currentvalue; + + let smash = strings.reduce(reducer); + + return smash; } // A local community center is holding a fund raising 5k fun run and has invited @@ -244,8 +255,10 @@ function stringSmash(/* code here */) { * @returns an array with all the runners' full names in the following format: "Smith, John". * The full names appear in the array in the same order the runners appear in the `runners` array. */ -function getFullNames(/* CODE HERE */) { - /* CODE HERE */ +function getFullNames(runners) { +let names = runners.map((n) => `${n.last_name}, ${n.first_name}`); + +return names; } /** @@ -260,8 +273,9 @@ function getFullNames(/* CODE HERE */) { * @returns an array with all the runners' first names in ALL CAPS. * The first names appear in the array in the same order the runners appear in the `runners` array. */ -function firstNamesAllCaps(/* CODE HERE */) { - /* CODE HERE */ +function firstNamesAllCaps(runners) { + let names = runners.map((n) => n.first_name.toUpperCase()); + return names; } /** @@ -308,9 +322,18 @@ function tallyUpDonations(/* CODE HERE */) { * * 1. What is the difference between counter1 and counter2? * + * In counter 1 all of the code is inside the function (local scope). In counter 2, + * the counter 2 function is using the count variable global scope inside it's function. * 2. Which of the two uses a closure? How can you tell? * + * The counter 2 is a closure. You can tell because of the global variable count is being used + * inside of the function counter2. unlike the counter 1 function which is doing everything inside + * of it's local scope. * 3. In what scenario would the counter1 code be preferable? In what scenario would counter2 be better? + * + * counter 1 would be preferable if want to used a set function. counter 2 would be good for functions, + * that is always going to have a certain chnage for the variables inside the function.Makes it so that the function + * is not hard coded. * */