Your Submissions
You haven't submitted any code for this challenge yet. Solve the problem by passing all the test cases, and your submissions will appear here.
xxxxxxxxxx
18
Array.prototype.myReduce = function (callback, initialValue) {
const argsLength = arguments.length;
//If array is empty and there is no initial value, return an error
if (argsLength === 1 && this.length === 0) {
throw new Error();
}
let index = argsLength === 1 ? 1 : 0;
let resultValue = argsLength === 1 ? this[0] : initialValue;
//Call the callback function for every element and replace the resultValue
for (let i = index; i < this.length; i += 1) {
resultValue = callback(resultValue, this[i], i, this);
}
return resultValue;
};
OUTPUT
Results will appear here.