diff --git a/index.js b/index.js index f61169392..23c8bfd2f 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,8 @@ 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) { + return callback(stringList[stringList.length -1]); } /** @@ -88,8 +88,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 +110,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); } /** @@ -155,8 +155,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 = []; + strings.forEach((string)=> { + lowerCase.push(string.toLowerCase()); + }) + return lowerCase; } /** @@ -174,8 +178,14 @@ function lowerCaseStrings(/* code here */) { * * [2] Invoking `isItAnApple` with `['a', 'b', 'c' ]` will return `[ false, false, false ]`. */ -function isItAnApple(/* code here */) { - /* code here */ +function isItAnApple(strings) { + const newString = strings.map((boolean) => { + if (boolean === 'apple') + return true; + else (boolean !== 'apple') + return false; + }); + return newString; } /** @@ -194,8 +204,8 @@ 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) { + return strings.filter(fruit => fruit !== 'apple'); } /** @@ -213,8 +223,8 @@ function removeApple(/* code here */) { * * [2] Invoking `stringSmash` with `['a', 'b', 'c' ]` will return `abc`. */ -function stringSmash(/* code here */) { - /* code here */ +function stringSmash(strings) { + return strings.reduce((total, item) => { return total += item }); } // A local community center is holding a fund raising 5k fun run and has invited @@ -232,8 +242,16 @@ 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) { + const fullNames = []; + + runners.forEach((runner) => { return fullNames.push(`${runner.last_name}, ${runner.first_name}`); }); + + return fullNames; + // const fullName = runners.map((name) => { + // return `${runners.last_name}, ${runners.first_name}`; + // }); + // return fullName; } /** @@ -248,8 +266,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) { + return runners.map((item) => { return item.first_name.toUpperCase() + }); } /** @@ -295,11 +314,16 @@ function tallyUpDonations(/* CODE HERE */) { * Study the code for counter1 and counter2. Answer the questions below. * * 1. What is the difference between counter1 and counter2? + * counter1 has a variable and a function nested inside of it. This function is different + * from counter 2 function because the nested function within can store and recall information + * that was processed. * * 2. Which of the two uses a closure? How can you tell? + * counter1 has a closure. I can tell because it has a nested function which accessed a variable + * outside of it. * * 3. In what scenario would the counter1 code be preferable? In what scenario would counter2 be better? - * + * counter1 would be preferable if you ......//////// */ // counter1 code