You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.4 KiB

5 years ago
## Reduce
### Instructions
Create four functions:
5 years ago
- `fold` that receives an array, a function and an accumulator, in this order,
and applies the function in the elements of the array starting on the left.
5 years ago
- `foldRight` that receives an array, a function and an accumulator, in this order,
and applies the function in the elements of the array starting on the right.
5 years ago
- `reduce` that works just like the method `[].reduce` when you don't
specify an accumulator.
The arguments should be an array and a function.
The starting value of your accumulator must be the first value of the array.
If your array is less than 1 argument you should throw an error.
5 years ago
- `reduceRight` like reduce, from the last value to the first
Example:
5 years ago
```js
const adder = (a, b) => a + b
fold([1, 2, 3], adder, 2) // returns 8 (2 + 1 + 2 + 3)
foldRight([1, 2, 3], adder, 2) // returns 8 (2 + 3 + 2 + 1)
reduce([1, 2, 3], adder) // returns 6 (1 + 2 + 3)
reduceRight([1, 2, 3], adder) // returns 6 (3 + 2 + 1)
5 years ago
```
The use of `[].reduce` and `[].reduceRight` is forbidden for this exercise.
### Notions
- [devdocs.io/javascript/global_objects/array/reduce](https://devdocs.io/javascript/global_objects/array/reduce)
5 years ago
### Code provided
> all code provided will be added to your solution and doesn't need to be submited.
5 years ago
```js
Array.prototype.reduce = undefined
Array.prototype.reduceRight = undefined
```