From 7975cbb285cbcacd054ec226825cf2d066de9f76 Mon Sep 17 00:00:00 2001 From: Golden Eagle Date: Tue, 25 Feb 2020 16:01:19 -0800 Subject: [PATCH 1/2] finished half way --- index.js | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 840bd1fce..2443adf9c 100644 --- a/index.js +++ b/index.js @@ -48,10 +48,14 @@ 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); } +// const newlist = (list) => { +// return list; +// } + /** * ### Challenge `processLastItem` * @@ -66,8 +70,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]); } /** @@ -87,8 +91,11 @@ function processLastItem(/* CODE HERE */) { * [2] Invoking `processSum` passing `[]` and `(num) => num + 1000`, * should return 1000. */ -function processSum(/* CODE HERE */) { - /* CODE HERE */ +function processSum(numberList, callback) { + var sum = numberList.reduce(function(sum, currentValue){ + return sum + currentValue; + }, 0); + return callback(sum); } /** @@ -109,8 +116,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); } /** @@ -133,10 +140,13 @@ function processProduct(/* CODE HERE */) { * "lady gaga" and `['foo', 'bar']` and `(bool) => bool ? 'nice!' : 'sad'`, * should return "sad". */ -function processContains(/* CODE HERE */) { - /* CODE HERE */ +function processContains(item, list, callback) { + if (list.includes(item)){ + return callback(true); + }return callback(false); } + /** * ### Challenge `processDuplicateFree` * THIS IS A STRETCH PROBLEM! ATTEMPT ONLY AFTER COMPLETING ALL NON-STRETCH CHALLENGES! @@ -156,13 +166,14 @@ function processContains(/* 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 */) { - /* CODE HERE ONLY AFTER COMPLETING ALL OTHER TASKS */ +function processDuplicateFree(list, callback) { } /////////////// HIGHER-ORDER ARRAY METHODS /////////////// /////////////// HIGHER-ORDER ARRAY METHODS /////////////// + + // A local community center is holding a fund raising 5k fun run and has invited // 50 small businesses to make a small donation on their behalf for some much needed // updates to their facilities. Each business has assigned a representative @@ -178,8 +189,11 @@ function processDuplicateFree(/* CODE HERE ONLY AFTER COMPLETING ALL OTHER TASKS * @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 newArray = []; + runners.forEach(function(items) { + newArray.push(`${items.last_name}, ${items.first_name}`); +}); return newArray; } /** From d93f93d4ac2553ce8ebabee686d09e90402d1794 Mon Sep 17 00:00:00 2001 From: Golden Eagle Date: Tue, 25 Feb 2020 20:32:28 -0800 Subject: [PATCH 2/2] finished everytthing --- index.js | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 2443adf9c..3d41c8981 100644 --- a/index.js +++ b/index.js @@ -167,6 +167,9 @@ function processContains(item, list, callback) { * should return 3. */ function processDuplicateFree(list, callback) { + return callback(list.filter((item, index) => { + return list.indexOf(item) === index; + })); } /////////////// HIGHER-ORDER ARRAY METHODS /////////////// @@ -208,8 +211,11 @@ function getFullNames(runners) { * @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 newArray = []; + runners.forEach(function(items) { + newArray.push(`${items.first_name.toUpperCase()}`); + }); return newArray; } /** @@ -225,8 +231,10 @@ function firstNamesAllCaps(/* CODE HERE */) { * @returns an array containing only the runners that use the given `tShirtSize`. * The runners in the array appear in the same order they appear in the `runners` array. */ -function getRunnersByTShirtSize(/* CODE HERE */) { - /* CODE HERE */ +function getRunnersByTShirtSize(runners, tShirtSize) { + // function (items) { + // runners.filter(items => items.shirt_size === tShirtSize){ + return runners.filter(items => items.shirt_size === tShirtSize); } /** @@ -239,10 +247,14 @@ function getRunnersByTShirtSize(/* CODE HERE */) { * @param runners array of runners like the one inside the /data/runners.js file. * @returns a number which is the sum of the donations by all runners. */ -function tallyUpDonations(/* CODE HERE */) { - /* CODE HERE */ +function tallyUpDonations(arr) { + return arr.reduce((acc , curr) => { + return acc += curr.donation + }, 0) + // return runners.reduce(function(accumulator, currentValue) { return accumulator.donation + currentvalue},0); } + /////////////// CLOSURES /////////////// /////////////// CLOSURES /////////////// @@ -259,15 +271,19 @@ function tallyUpDonations(/* CODE HERE */) { * counter() // should return 2 * etc */ + function counterMaker() { // BROKEN CODE STARTS - const count = 0; - function counter() { - ++count + let count = 0; + function counterPlusOne(){ + return count++; + } + return counterPlusOne; // BROKEN CODE ENDS } + /** * ### Challenge `counterMakerWithLimit` * @@ -288,8 +304,15 @@ function counterMaker() { * counter() // should return 0 * etc */ -function counterMakerWithLimit(/* CODE HERE */) { - /* CODE HERE */ +function counterMakerWithLimit(limit) { + let count = 0; + function counterPlusOne(){ + if(count > limit) { + count = 0; + } + return count++; + } + return counterPlusOne; } /////////////// END OF CHALLENGE ///////////////