fix(curriculum): Rewrote the Write Higher-Order Arrow Functions challenge and moved to Functional Programming section (#35954)
* fix: reworded and moved challenge * fix: removed extra line Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * fix; removed extra line Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com> * fix: reworded sentence Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
---
|
||||
title: Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem
|
||||
---
|
||||
 Remember to use <a>**`Read-Search-Ask`**</a> if you get stuck. Try to pair program  and write your own code 
|
||||
|
||||
### Problem explanation:
|
||||
|
||||
We need to compute and square values from the `realNumberArray` and store them in the variable `squaredIntegers` using the `map()`, `filter()`, and/or `reduce()` functions.
|
||||
|
||||
##  Hint: 1
|
||||
|
||||
* You will need to `filter()` the `realNumberArray` for positive integers (decimals are not integers).
|
||||
|
||||
> _try to solve the problem now_
|
||||
|
||||
##  Hint: 2
|
||||
|
||||
* You will need to `map()` the values from your `filter()` function to the variable `squaredIntegers`.
|
||||
|
||||
> _try to solve the problem now_
|
||||
|
||||
##  Hint: 3
|
||||
|
||||
* Remember the magic of chaining functions.
|
||||
|
||||
> _try to solve the problem now_
|
||||
|
||||
## Spoiler alert!
|
||||
|
||||

|
||||
|
||||
**Solution ahead!**
|
||||
|
||||
##  Code solution:
|
||||
```js
|
||||
const squareList = (arr) => arr
|
||||
.filter((num) => num > 0 && num % parseInt(num) === 0)
|
||||
.map((num) => Math.pow(num, 2));
|
||||
```
|
||||
|
||||
### Code explanation:
|
||||
|
||||
Uses the operator `filter()` and `map()` functions to square all positive integers in a given array.
|
||||
|
||||
## Alternative code solution:
|
||||
```js
|
||||
const squareList = (arr) => {
|
||||
return arr.reduce((sqrIntegers, num) => {
|
||||
return Number.isInteger(num) && num > 0
|
||||
? sqrIntegers.concat(num * num)
|
||||
: sqrIntegers;
|
||||
}, []);
|
||||
};
|
||||
```
|
||||
|
||||
### Resources
|
||||
|
||||
- ["Array.prototype.map()" - *MDN JavaScript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
|
||||
- ["Array.prototype.filter()" - *MDN JavaScript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)
|
||||
- ["Array.prototype.reduce()" - *MDN JavaScript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce)
|
||||
- ["Number.isInteger()" - *MDN JavaScript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger)
|
||||
- ["Math.pow()" - *MDN JavaScript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow)
|
Reference in New Issue
Block a user