Array.prototype.reduce
Implement a custom version of the Array.prototype.reduce method and add it to the Array.prototype object as myReduce. The method should iterate over the array, apply a reducer function to each element, and return a single accumulated value.
This function should mimic the behavior of the native reduce() method, including the handling of an optional initial value.
Example Inputs & Outputs
[1, 2, 3].myReduce((acc, val) => acc + val) // → 6 [1, 2, 3].myReduce((acc, val) => acc + val, 10) // → 16 [].myReduce((acc, val) => acc + val, 5) // → 5 [].myReduce((acc, val) => acc + val) // → TypeError [1, , 3].myReduce((acc, val) => acc + val) // → 4
Constraints & Edge Cases
callbackmust be a function. If not, throw aTypeError.- If no initial value is provided and the array is empty, throw a
TypeError. - If no initial value is provided, use the first element of the array as the initial value, and start from the second element.
- Do not use the built-in
reduce()method.
Companies:
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
