Advertisement
vinissh

reduce3.js

Jan 18th, 2021 (edited)
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Array.prototype.reduce  =  function(callback, valorInicial){
  2.     const indiceInicial = valorInicial ? 0 : 1
  3.     let acumulador = valorInicial || this[0]
  4.     for(let i = indiceInicial; i < this.length; i++) {
  5.         acumulador = callback(acumulador, this[i], i, this)
  6.     }
  7.     return acumulador
  8. }
  9.  
  10. const soma = (total, valor) => total + valor
  11. const nums = [ 1, 2, 3, 4, 5, 6]
  12. console.log(nums.reduce(soma))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement