Testsigma Interview Question

JavaScript - Polyfill for reduce()

Interview Answer

Anonymous

Apr 26, 2025

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