Domanda di colloquio di Adobe

Create a polyfill for array reduce

Risposte di colloquio

Anonimo

5 lug 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; }

Anonimo

5 lug 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; };