Loading challenge...
Preparing the challenge details...
Loading challenge...
Preparing the challenge details...
Implement Array.prototype.reduce from scratch, including the tricky no-initial-value path and the empty-array error case.
reduce is the hardest of the three array iterators to polyfill correctly. When no initial value is provided, the first element becomes the accumulator and iteration starts at index 1. Calling reduce on an empty array with no initial value must throw a TypeError. The subtle bug most candidates write: checking initialValue === undefined instead of arguments.length >= 2.
Goal: Implement myReduce with correct handling of the missing-initialValue path and the empty-array error.
Continue learning with these related challenges
Implement Array.prototype.map from scratch. Master callback signatures, thisArg binding, and sparse-array hole preservation.
Implement Array.prototype.filter from scratch. The key detail: push the original element, not the boolean result of the predicate.
Implement a function to flatten a nested array in JavaScript.
Implement Array.prototype.map from scratch. Master callback signatures, thisArg binding, and sparse-array hole preservation.
Implement Array.prototype.filter from scratch. The key detail: push the original element, not the boolean result of the predicate.
Implement a function to flatten a nested array in JavaScript.