Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- if second argument is not passed, the initial value is the first item in the array
- */
- let arr = [1, 2, 3];
- Array.prototype.reduce = function(combinator, initialValue) {
- let = accumulator = 0;
- let counter = initialValue ? 0 : 1;
- initialValue ? accumulator = initialValue : accumulator = this[0];
- while(counter < this.length) {
- accumulator = combinator(accumulator, this[counter])
- counter++;
- }
- return accumulator;
- }
- a = arr.reduce((acc, val) => acc + val, 2)
- console.log(a)
Add Comment
Please, Sign In to add comment