diff --git a/curriculum/challenges/_meta/es6/meta.json b/curriculum/challenges/_meta/es6/meta.json index 62758afbb7..bf1040aff0 100644 --- a/curriculum/challenges/_meta/es6/meta.json +++ b/curriculum/challenges/_meta/es6/meta.json @@ -36,10 +36,6 @@ "587d7b88367417b2b2512b44", "Write Arrow Functions with Parameters" ], - [ - "587d7b88367417b2b2512b45", - "Write Higher Order Arrow Functions" - ], [ "587d7b88367417b2b2512b46", "Set Default Parameters for Your Functions" diff --git a/curriculum/challenges/_meta/functional-programming/meta.json b/curriculum/challenges/_meta/functional-programming/meta.json index 8ac477d744..d70b441bba 100644 --- a/curriculum/challenges/_meta/functional-programming/meta.json +++ b/curriculum/challenges/_meta/functional-programming/meta.json @@ -68,6 +68,10 @@ "587d7da9367417b2b2512b68", "Use the reduce Method to Analyze Data" ], + [ + "587d7b88367417b2b2512b45", + "Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem" + ], [ "587d7da9367417b2b2512b69", "Sort an Array Alphabetically using the sort Method" diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/write-higher-order-arrow-functions.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/write-higher-order-arrow-functions.english.md deleted file mode 100644 index e89272e57b..0000000000 --- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/write-higher-order-arrow-functions.english.md +++ /dev/null @@ -1,124 +0,0 @@ ---- -id: 587d7b88367417b2b2512b45 -title: Write Higher-Order Arrow Functions -challengeType: 1 ---- - -## Description -
-It's time to look at higher-order functions and their common pair, arrow functions. -Arrow functions work really well when combined with higher-order functions, such as map(), filter(), and reduce().
-But what are these functions? Lets look at the simplest example forEach(), and run it on the following array of sample Facebook posts. -
let FBPosts = [
- {thumbnail: "someIcon", likes:432, shares: 600},
- {thumbnail: "Another icon", likes:300, shares: 501},
- {thumbnail: "Yet another", likes:40, shares: 550},
- {thumbnail: null, likes: 101, shares:0},
-]
-
-Of the two forEach() versions below, both perform the exact same log function, and each takes an anonymous callback with a parameter post. The difference is the syntax. One uses an arrow function and the other does not. -
-ES5
-FBpost.forEach(function(post) {
- console.log(post) // log each post here
- });
-ES6
-FBpost.forEach((post) => {
- console.log(post) // log each post here
- });
- -
-filter() is very similar. Below it will iterate over the FBPosts array, perform the logic to filter out the items that do not meet the requirements, and return a new array, results. - -
-let results = arr1.filter((post) => { - return post.thumbnail !== null && post.likes > 100 && post.shares > 500 -});

-console.log(results); // [{thumbnail: "someIcon", likes: 432, shares: 600}, {thumbnail: "Another icon", likes: 300, shares: 501}] -
- -
- - - -## Instructions -
-Use arrow function syntax to compute the square of only the positive integers (decimal numbers are not integers) in the array realNumberArray and store the new array in the variable squaredIntegers. -
- -## Tests -
- -```yml -tests: - - text: squareList should be a function. - testString: assert.typeOf(squareList, 'function'), 'squareList should be a function'; - - text: squareList should be a constant variable (by using const). - testString: getUserInput => assert(getUserInput('index').match(/const\s+squaredIntegers/g), 'squaredIntegers should be a constant variable (by using const).'); - - text: function keyword was not used. - testString: getUserInput => assert(!getUserInput('index').match(/function/g), 'function keyword was not used.'); - - text: loop should not be used - testString: getUserInput => assert(!getUserInput('index').match(/(for)|(while)/g), 'loop should not be used'); - - text: map, filter, or reduce should be used - testString: getUserInput => assert(getUserInput('index').match(/map|filter|reduce/g), 'map, filter, or reduce should be used'); - - text: The function should return an array called squaredIntegers - testString: assert(Array.isArray(squaredIntegers), 'squaredIntegers should be an array'); - - text: squaredIntegers should be [16, 1764, 36] - testString: assert.deepStrictEqual(squaredIntegers, [16, 1764, 36], 'squaredIntegers should be [16, 1764, 36]'); - -``` - -
- -## Challenge Seed -
- -
- -```js -const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2]; -const squareList = (arr) => { - "use strict"; - const positiveIntegers = arr.filter((num) => { - // add code here - }); - const squaredIntegers = positiveIntegers.map((num) => { - // add code here - }); - - return squaredIntegers; -}; -// test your code -const squaredIntegers = squareList(realNumberArray); -console.log(squaredIntegers); - -``` - -
- - - -
- -## Solution -
- -```js -const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2]; -const squareList = (arr) => { - "use strict"; - const positiveIntegers = arr.filter((num) => { - return num >= 0 && Number.isInteger(num); - // add code here - }); - const squaredIntegers = positiveIntegers.map((num) => { - // add code here - return num ** 2; - }); - // add code here - return squaredIntegers; -}; -// test your code -const squaredIntegers = squareList(realNumberArray); -``` -
diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/use-higher-order-functions-map-filter-or-reduce-to-solve-a-complex-problem.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/use-higher-order-functions-map-filter-or-reduce-to-solve-a-complex-problem.english.md new file mode 100644 index 0000000000..3a1323dab2 --- /dev/null +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/use-higher-order-functions-map-filter-or-reduce-to-solve-a-complex-problem.english.md @@ -0,0 +1,82 @@ +--- +id: 587d7b88367417b2b2512b45 +title: Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem +challengeType: 1 +--- + +## Description +
+Now that you have worked through a few challenges using higher-order functions like map(), filter(), and reduce(), you now get to apply them to solve a more complex challenge. +
+ +## Instructions +
+We have defined a function named squareList. You need to complete the code for the squareList function using any combination of map(), filter(), and reduce() so that it returns a new array containing only the square of only the positive integers (decimal numbers are not integers) when an array of real numbers is passed to it. An example of an array containing only real numbers is [-3, 4.8, 5, 3, -3.2]. +Note: Your function should not use any kind of for or while loops or the forEach() function. +
+ +## Tests +
+ +```yml +tests: + - text: squareList should be a function. + testString: assert.typeOf(squareList, 'function'), 'squareList should be a function'; + - text: for or while loops or forEach should not be used. + testString: assert(!removeJSComments(code).match(/for|while|forEach/g)); + - text: map, filter, or reduce should be used. + testString: assert(removeJSComments(code).match(/\.(map|filter|reduce)\s*\(/g)); + - text: The function should return an array. + testString: assert(Array.isArray(squareList([4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2]))); + - text: squareList([4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2]) should return [16, 1764, 36]. + testString: assert.deepStrictEqual(squareList([4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2]), [16, 1764, 36]); + - text: squareList([-3.7, -5, 3, 10, 12.5, 7, -4.5, -17, 0.3]) should return [9, 100, 49]. + testString: assert.deepStrictEqual(squareList([-3.7, -5, 3, 10, 12.5, 7, -4.5, -17, 0.3]), [9, 100, 49]); +``` + +
+ +## Challenge Seed +
+ +
+ +```js +const squareList = (arr) => { + // only change code below this line + return arr; + // only change code above this line +}; + +// test your code +const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]); +console.log(squaredIntegers); +``` + +
+ +
+ +```js +const removeJSComments = str => str.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, ''); +``` + +
+ +
+ +## Solution +
+ +```js +const squareList = (arr) => { + const positiveIntegers = arr.filter(num => { + return num >= 0 && Number.isInteger(num); + }); + const squaredIntegers = positiveIntegers.map(num => { + return num ** 2; + }); + return squaredIntegers; +}; +``` +
diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/es6/write-higher-order-arrow-functions/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/functional-programming/use-higher-order-functions-map-filter-or-reduce-to-solve-a-complex-problem/index.md similarity index 75% rename from guide/english/certifications/javascript-algorithms-and-data-structures/es6/write-higher-order-arrow-functions/index.md rename to guide/english/certifications/javascript-algorithms-and-data-structures/functional-programming/use-higher-order-functions-map-filter-or-reduce-to-solve-a-complex-problem/index.md index 00b51f4fc0..ea754c6109 100644 --- a/guide/english/certifications/javascript-algorithms-and-data-structures/es6/write-higher-order-arrow-functions/index.md +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/functional-programming/use-higher-order-functions-map-filter-or-reduce-to-solve-a-complex-problem/index.md @@ -1,5 +1,5 @@ --- -title: Write Higher Order Arrow Functions +title: Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem --- ![:triangular_flag_on_post:](https://forum.freecodecamp.com/images/emoji/emoji_one/triangular_flag_on_post.png?v=3 ":triangular_flag_on_post:") Remember to use **`Read-Search-Ask`** if you get stuck. Try to pair program ![:busts_in_silhouette:](https://forum.freecodecamp.com/images/emoji/emoji_one/busts_in_silhouette.png?v=3 ":busts_in_silhouette:") and write your own code ![:pencil:](https://forum.freecodecamp.com/images/emoji/emoji_one/pencil.png?v=3 ":pencil:") @@ -32,35 +32,26 @@ We need to compute and square values from the `realNumberArray` and store them i **Solution ahead!** ## ![:beginner:](https://forum.freecodecamp.com/images/emoji/emoji_one/beginner.png?v=3 ":beginner:") Code solution: -```javascript - const squareList = (arr) => { - "use strict"; - const squaredIntegers = arr.filter((num) => num > 0 && num % parseInt(num) === 0).map((num) => Math.pow(num, 2)); - return squaredIntegers; - }; - - // test your code - const squaredIntegers = squareList(realNumberArray); - console.log(squaredIntegers); +```js +const squareList = (arr) => arr + .filter((num) => num > 0 && num % parseInt(num) === 0) + .map((num) => Math.pow(num, 2)); ``` -- [Run code at codepen.io](https://codepen.io/dylantyates/pen/WyWoYJ) + ### Code explanation: Uses the operator `filter()` and `map()` functions to square all positive integers in a given array. - ## Alternative code solution: -```javascript - - // change code below this line - const squaredIntegers = arr.filter((param) => Number.isInteger(param) && param >= 0).map(x => x * x); - // change code above this line - +```js +const squareList = (arr) => { + return arr.reduce((sqrIntegers, num) => { + return Number.isInteger(num) && num > 0 + ? sqrIntegers.concat(num * num) + : sqrIntegers; + }, []); +}; ``` -- [Run code at repl.it](https://repl.it/@AdrianSkar/ES6-Write-higher-order-arrow-functions) -### Code explanation -This does basically the same but uses the `isInteger()` method to check the numbers. - ### Resources