From 77eaccc5d896894c85cf947185621adb29d3112f Mon Sep 17 00:00:00 2001 From: GusCohen Date: Mon, 27 Apr 2020 19:03:09 -0300 Subject: [PATCH] updated files --- index.js | 193 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 109 insertions(+), 84 deletions(-) diff --git a/index.js b/index.js index f61169392..aa7b0cf72 100644 --- a/index.js +++ b/index.js @@ -2,14 +2,14 @@ /** * ### Challenge `processFirstItem` - * + * * @instructions * Implement a higher-order function called `processFirstItem`. * It takes two arguments: * @param stringList an array of strings. * @param callback function that takes a string as its argument. * @returns the result of invoking `callback` with the FIRST element in `stringList`. - * + * * Example of usage of this higher-order function: * Invoking `processFirstItem` passing `['foo', 'bar']` and `(str) => str + str`, * should return 'foofoo'. @@ -30,49 +30,49 @@ function processFirstItem(stringList, callback) { /** * ### Challenge `processLength` - * + * * @instructions * PLEASE STUDY THE EXAMPLE CHALLENGE THOROUGHLY BEFORE PROCEEDING! * PLEASE STUDY THE EXAMPLE CHALLENGE THOROUGHLY BEFORE PROCEEDING! - * + * * Implement a higher-order function called `processLength`. * It takes two arguments: * @param list an array with elements of any type. * @param callback function that takes a number as its argument. * @returns the result of invoking `callback` passing the LENGTH of `list`. - * + * * Examples of usage of this higher-order function: * [1] Invoking `processLength` passing `['foo', 'bar']` and `(num) => num + 1000`, * should return 1002. - * + * * [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); } /** * ### Challenge `processLastItem` - * + * * @instructions * Implement a higher-order function called `processLastItem`. * It takes two arguments: * @param stringList array of strings. * @param callback function that takes a string as its argument. * @returns the result of invoking `callback` with the LAST element in `stringList`. - * + * * Example of usage of this higher-order function: * 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]); } /** * ### Challenge `processSum` - * + * * @instructions * Implement a higher-order function called `processSum`. * It takes three arguments: @@ -80,21 +80,21 @@ function processLastItem(/* CODE HERE */) { * @param num2 a number. * @param callback function that takes a number as its argument. * @returns the result of invoking `callback` passing the SUM of `num1` and `num2`. - * + * * Examples of usage of this higher-order function: * [1] Invoking `processSum` passing `10`, `30` and `(num) => num + " is a big number!"`, * should return "40 is a big number!". - * + * * [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); } /** * ### Challenge `processProduct` - * + * * @instructions * Implement a higher-order function called `processProduct`. * It takes three arguments: @@ -102,38 +102,39 @@ function processSum(/* CODE HERE */) { * @param num2 a number. * @param callback function that takes a number as its argument. * @returns the result of invoking `callback` passing the PRODUCT of `num1` and `num2`. - * + * * Examples of usage of this higher-order function: * [1] Invoking `processProduct` passing 2 and 7 and `(num) => num + " is a big number!"`, * should return "14 is a big number!". - * + * * [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); } /** * ### Challenge `processDuplicateFree` * THIS IS A STRETCH PROBLEM! ATTEMPT ONLY AFTER COMPLETING ALL NON-STRETCH CHALLENGES! - * + * * @instructions * Implement a higher-order function called `processDuplicateFree`. * It takes two arguments: * @param list array of elements of any kind. * @param callback function that takes an array as its argument. * @returns the result of invoking `callback` passing a de-duped version of `list`. - * + * * Examples of usage of this higher-order function: * [1] Invoking `processDuplicateFree` passing `[1,1,1,2]` and `(arr) => arr`, * should return `[1,2]`. - * + * * [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) { + const newArray = list.filter((a, b) => list.indexOf(a) === b); + return callback(newArray); } /////////////// HIGHER-ORDER ARRAY METHODS /////////////// @@ -142,80 +143,90 @@ function processDuplicateFree(/* CODE HERE ONLY AFTER COMPLETING ALL OTHER TASKS /** * ### Challenge `lowerCaseStrings` - * + * * @instructions * Implement this function using forEach(). - * + * * @param strings an array of strings. * @returns an array of equal length to `strings` containing lowercased versions of each string. - * - * + * + * * Examples of usage of this function: * [1] Invoking `lowerCaseStrings` with `[ 'Orange', 'APPLE', 'banana', 'mAnGo']` will return `[ 'orange', 'apple', 'banana', 'mango' ]`. - * + * * [2] Invoking `lowerCaseStrings` with `['a', 'b', 'c' ]` will return `[ 'a', 'b', 'c' ]`. */ -function lowerCaseStrings(/* code here */) { - /* code here */ +function lowerCaseStrings(strings) { + const lowerCase = []; + strings.forEach(string => lowerCase.push(string.toLowerCase())); + return lowerCase; } /** * ### Challenge `isItAnApple` - * + * * @instructions * Implement this function using map(). - * + * * @param strings an array of strings. * @returns an array of equal length to `strings` containing `true` if the corresponding entry in the `strings` is 'apple' and `false` if it is anything else. - * - * + * + * * Examples of usage of this function: * [1] Invoking `isItAnApple` with `[ 'orange', 'apple', 'banana', 'apples', 'apple', 'mango' ]` will return `[ false, true, false, false, true, false ]`. - * + * * [2] Invoking `isItAnApple` with `['a', 'b', 'c' ]` will return `[ false, false, false ]`. */ -function isItAnApple(/* code here */) { - /* code here */ +function isItAnApple(strings) { + return strings.map((item) => { + if (item === "apple") { + return strings = true; + } else { + return strings = false; + } + }) } /** * ### Challenge `removeApple` - * + * * @instructions * Implement this function using filter(). - * + * * @param strings an array of strings. * @returns a similar array, with an entries that are 'apple' removed. - * + * *This function is case sensitive and, for example, should not remove 'Apple' or 'APPLE' - * + * * Examples of usage of this function: * [1] Invoking `removeApple` with `[ 'orange', 'apple', 'banana', 'apples', 'apple', 'mango' ]` will return `[ 'orange', 'banana', 'apples', 'mango' ]`. - * + * * [2] Invoking `removeApple` with `['a', 'b', 'c' ]` will return `[ 'a', 'b', 'c' ]`. */ -function removeApple(/* code here */) { - /* code here */ -} +function removeApple(strings) { + return strings.filter(string => string !== "apple"); +}; /** * ### Challenge `stringSmash` - * + * * @instructions * Implement this function using reduce(). Do NOT use any other array methods. - * + * * @param strings an array of strings. * @returns a string with all entries in `strings` combined together. - * - * + * + * * Examples of usage of this function: * [1] Invoking `stringSmash` with `[ 'orange', 'apple', 'banana', 'apples', 'apple', 'mango' ]` will return 'orangeapplebananaapplesapplemango'. - * + * * [2] Invoking `stringSmash` with `['a', 'b', 'c' ]` will return `abc`. */ -function stringSmash(/* code here */) { - /* code here */ -} +function stringSmash(strings) { + return strings.reduce((accum, string) => { + return accum + string; + }); +}; // 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 @@ -224,81 +235,89 @@ function stringSmash(/* code here */) { /** * ### Challenge `getFullNames` - * + * * @instructions * Implement this function using forEach() or map(). - * + * * @param runners array of runners like the one inside the /data/runners.js file. * @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 fullNames = []; + runners.forEach(runner => + fullNames.push(`${runner.last_name}, ${runner.first_name}`) + ); + return fullNames; } /** * ### Challenge `firstNamesAllCaps` - * + * * @instructions - * The event director needs to have all the runners' first names + * The event director needs to have all the runners' first names * in uppercase because the director BECAME DRUNK WITH POWER. * Implement this function using map(). - * + * * @param runners array of runners like the one inside the /data/runners.js file. * @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(runner => runner.first_name.toUpperCase()); } + + /** * ### Challenge `getRunnersByTShirtSize` * * THIS IS A STRETCH PROBLEM! ATTEMPT ONLY AFTER COMPLETING ALL NON-STRETCH CHALLENGES! - * + * * @instructions * The event director needs a way to find the runners that need * a specific t-shirt size, so they can place the orders easily. * Implement this function using filter(). - * + * * @param runners array of runners like the one inside the /data/runners.js file. * @param tShirtSize string (possible values are "S", "M", "L", "XL", "2XL", "3XL"). * @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) { + return runners.filter(runner => runner.shirt_size === tShirtSize); } /** * ### Challenge `tallyUpDonations` * * THIS IS A STRETCH PROBLEM! ATTEMPT ONLY AFTER COMPLETING ALL NON-STRETCH CHALLENGES! - * + * * @instructions * The donations need to be tallied up and reported for tax purposes. * Implement this function using reduce(). - * + * * @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(runners) { + return runners.reduce((accum, runner) => accum + runner.donation, 0); } + + /////////////// CLOSURES /////////////// /////////////// CLOSURES /////////////// /** * ### Challenge `counterMaker` - * + * * @instructions * Study the code for counter1 and counter2. Answer the questions below. - * + * * 1. What is the difference between counter1 and counter2? - * + * * 2. Which of the two uses a closure? How can you tell? - * - * 3. In what scenario would the counter1 code be preferable? In what scenario would counter2 be better? +* I think Counter 1 uses a closure. Because the counter function relies on the count variable defined outside of it + * 3. In what scenario would the counter1 code be preferable? In what scenario would counter2 be better? * */ @@ -322,12 +341,12 @@ function counter2() { /** * ### Challenge `counterMakerWithLimit` * THIS IS A STRETCH PROBLEM! ATTEMPT ONLY AFTER COMPLETING ALL NON-STRETCH CHALLENGES! - * + * * @instructions * Implement a counter maker that takes a max value for the count. * A counter created with it will reset itself after reaching the max value. * Usage is as follows: - * + * * const counter = counterMakerWithLimit(3) * counter() // should return 0 * counter() // should return 1 @@ -340,8 +359,14 @@ function counter2() { * counter() // should return 0 * etc */ -function counterMakerWithLimit(/* CODE HERE */) { - /* CODE HERE */ +function counterMakerWithLimit(max) { + let count = 0; + return function () { + if (count > max) { + count = 0; + } + return count++; + }; } /////////////// END OF CHALLENGE /////////////// @@ -358,7 +383,7 @@ if (typeof exports !== 'undefined') { if (processSum) { module.exports.processSum = processSum } if (processProduct) { module.exports.processProduct = processProduct } if (processDuplicateFree) { module.exports.processDuplicateFree = processDuplicateFree } - if (lowerCaseStrings ) { module.exports.lowerCaseStrings = lowerCaseStrings} + if (lowerCaseStrings) { module.exports.lowerCaseStrings = lowerCaseStrings } if (isItAnApple) { module.exports.isItAnApple = isItAnApple } if (removeApple) { module.exports.removeApple = removeApple } if (stringSmash) { module.exports.stringSmash = stringSmash }