public/subjects/using-reduce/README.md

26 lines
752 B
Markdown
Raw Permalink Normal View History

2020-05-29 08:59:50 +00:00
## Using Reduce
### Instructions
2020-05-29 08:59:50 +00:00
2022-05-15 18:26:29 +00:00
Create the following functions:
2021-04-27 19:04:54 +00:00
2022-05-15 18:26:29 +00:00
> Your solutions **must** use `reduce`.
2020-05-29 08:59:50 +00:00
2022-05-15 18:26:29 +00:00
- `adder`: accepts an array of numbers, and returns the sum as a `number`.
2020-05-29 08:59:50 +00:00
2022-05-15 18:26:29 +00:00
- `sumOrMul`: accepts an array of numbers and adds or multiplies its elements depending on whether the element is odd or even. Even = multiply. Odd = add.
2020-05-29 08:59:50 +00:00
2022-05-15 18:26:29 +00:00
- `funcExec`: accepts an array of functions and executes them using `reduce`, returning the result.
2020-05-29 08:59:50 +00:00
2022-05-15 18:26:29 +00:00
> Each function may accept an optional argument, which should be the initial value for the function's execution.
2021-04-27 19:04:54 +00:00
2022-05-15 18:26:29 +00:00
#### Example:
2022-05-15 18:26:29 +00:00
```js
sumOrMul([1, 2, 3, 5, 8], 5) // (((((5 + 1) * 2) + 3) + 5) * 8) -> 160
```
2020-05-29 08:59:50 +00:00
### Notions
2022-05-15 18:26:29 +00:00
- [Array.prototype.reduce](https://devdocs.io/javascript/global_objects/array/reduce)