Advertisement
nikolayneykov92

Untitled

Jun 3rd, 2019
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve (arr) {
  2.   Object.getPrototypeOf(arr).myReduce = function (func, initialVal) {
  3.     let accumolator = initialVal
  4.  
  5.     for (let i = 0; i < this.length; i++) {
  6.       accumolator = func(accumolator, this[i])
  7.     }
  8.  
  9.     return accumolator
  10.   }
  11.  
  12.   let sum = arr.myReduce((a, b) => a + b, 0)       // 6
  13.   let subtract = arr.myReduce((a, b) => a - b, 10) // 4
  14.   let divide = arr.myReduce((a, b) => a / b, 12)   // 2
  15.   let multiply = arr.myReduce((a, b) => a * b, 1)  // 6
  16.  
  17.   console.log(sum)
  18.   console.log(subtract)
  19.   console.log(divide)
  20.   console.log(multiply)
  21. }
  22.  
  23. solve([1, 2, 3])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement