Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve (arr) {
- Object.getPrototypeOf(arr).myReduce = function (func, initialVal) {
- let accumolator = initialVal
- for (let i = 0; i < this.length; i++) {
- accumolator = func(accumolator, this[i])
- }
- return accumolator
- }
- let sum = arr.myReduce((a, b) => a + b, 0) // 6
- let subtract = arr.myReduce((a, b) => a - b, 10) // 4
- let divide = arr.myReduce((a, b) => a / b, 12) // 2
- let multiply = arr.myReduce((a, b) => a * b, 1) // 6
- console.log(sum)
- console.log(subtract)
- console.log(divide)
- console.log(multiply)
- }
- solve([1, 2, 3])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement