Guest User

Untitled

a guest
Jan 23rd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. /*
  2. if second argument is not passed, the initial value is the first item in the array
  3. */
  4. let arr = [1, 2, 3];
  5.  
  6. Array.prototype.reduce = function(combinator, initialValue) {
  7. let = accumulator = 0;
  8. let counter = initialValue ? 0 : 1;
  9. initialValue ? accumulator = initialValue : accumulator = this[0];
  10.  
  11. while(counter < this.length) {
  12. accumulator = combinator(accumulator, this[counter])
  13. counter++;
  14. }
  15.  
  16. return accumulator;
  17. }
  18. a = arr.reduce((acc, val) => acc + val, 2)
  19.  
  20. console.log(a)
Add Comment
Please, Sign In to add comment