xpetit
def316d0f3
|
4 years ago | |
---|---|---|
.. | ||
README.md | 4 years ago |
README.md
Reduce
Instructions
Create three functions:
-
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. -
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. -
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. -
reduceRight
like reduce, from the last value to the first
Example:
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)
The use of [].reduce
and [].reduceRight
is forbidden for this exercise.
Notions
Code provided
all code provided will be added to your solution and doesn't need to be submited.
Array.prototype.reduce = undefined
Array.prototype.reduceRight = undefined