Pergunta de entrevista da empresa Adobe

Create a polyfill for array reduce

Respostas da entrevista

Sigiloso

5 de jul. de 2022

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

Sigiloso

5 de jul. de 2022

Below solution works for me: Array.prototype.myReduce = function (callback, initialValue) { let accumulator; let firstIndex; if (arguments.length === 1) { accumulator = this[0]; firstIndex = 1; } else { accumulator = initialValue; firstIndex = 0; } for (let index = firstIndex; index < this.length; index++) { accumulator = callback(accumulator, this[index], index, this); } return accumulator; };