DEV Community

Discussion on: Polyfill for Array.reduce() Javascript

Collapse
 
c4r4x35 profile image
Srinivas Kandukuri

It's almost same, Incase if anyone wants in simple form

Array.prototype.myReduce = function (callback, initialValue) {
let accumulator = initialValue === undefined ? undefined : initialValue;
for (let i = 0; i < this.length; i++) {
if (accumulator != undefined)
accumulator = callback(this[i], i, this)
else
accumulator = this[i];
}
return accumulator;
}

Collapse
 
melvdouc profile image
Melvin DOUCET • Edited

let accumulator = initialValue === undefined ? undefined : initialValue;

In other words:
let accumulator = initialValue;