DEV Community

Discussion on: Polyfill for Array.reduce() Javascript

Collapse
 
surajpandey186 profile image
Suraj Bhushan Pandey

Array.prototype._reduce = function (callback, initial) {

if (initial) {
    previous = initial;

   for (let i = 0; i < this.length; ++ i) {
      previous = callback(previous, this[i], i, this)
   }

} else {
    previous = this[0]
    for (let i = 1; i < this.length; ++ i) {
        previous = callback(previous, this[1], i, this)
    }
}
return previous
Enter fullscreen mode Exit fullscreen mode

}

Collapse
 
amansaxena001 profile image
Amansaxena001

in else part it should be this[i+1] not this[1]