Implementing Array.prototype.reduce in JavaScript
The reduce() method is a powerful tool in JavaScript for transforming an array into a single value. It iterates through the array, applying a callback function to accumulate a result. Your task is to implement a custom version of Array.prototype.reduce to deepen your understanding of array manipulation and functional programming principles.
Problem Description
You are to implement a function that extends the Array.prototype with a reduce method. This method should take two arguments:
callback: A function to execute on each element in the array. This function should accept three arguments:accumulator: The accumulated value, which is initially theinitialValue(see below) or the first element of the array if noinitialValueis provided.currentValue: The current element being processed in the array.currentIndex: The index of the current element being processed.array: The arrayreducewas called upon.
initialValue(optional): An initial value to be used as the accumulator on the first call to the callback. If not provided, the first element of the array will be used as the initial accumulator value, and the iteration will start from the second element.
The reduce method should return the final accumulated value after iterating through all elements of the array.
Key Requirements:
- The implementation must correctly handle both cases: when an
initialValueis provided and when it is not. - The callback function must be executed for each element in the array in the correct order.
- The accumulator must be updated correctly in each iteration.
- The method should not modify the original array.
- The method should handle empty arrays correctly (see edge cases).
Examples
Example 1:
Input: [1, 2, 3, 4].reduce((accumulator, currentValue) => accumulator + currentValue, 0)
Output: 10
Explanation: The callback function sums the accumulator and the current value. Starting with an initial accumulator of 0, the function calculates: 0 + 1 = 1, 1 + 2 = 3, 3 + 3 = 6, 6 + 4 = 10.
Example 2:
Input: [1, 2, 3, 4].reduce((accumulator, currentValue) => accumulator * currentValue)
Output: 24
Explanation: The callback function multiplies the accumulator and the current value. Since no initialValue is provided, the accumulator starts with the first element (1), and the function calculates: 1 * 2 = 2, 2 * 3 = 6, 6 * 4 = 24.
Example 3:
Input: [].reduce((accumulator, currentValue) => accumulator + currentValue, 0)
Output: 0
Explanation: The array is empty. Since an initialValue is provided, it is returned directly.
Example 4:
Input: [5].reduce((accumulator, currentValue) => accumulator + currentValue)
Output: 5
Explanation: The array has only one element. Since no initialValue is provided, the accumulator starts with the first element (5), and the function returns 5.
Constraints
- The input array can contain any type of data.
- The callback function can return any type of data.
- The
initialValuecan be of any type. - The array will not contain circular references that would cause infinite loops.
- The implementation should be efficient and avoid unnecessary iterations.
Notes
- Consider how to handle the case where the array has only one element and no
initialValueis provided. - Think about the order of execution when no
initialValueis provided. - Remember that
reduceshould not modify the original array. - This is a good exercise to understand how higher-order functions and closures work in JavaScript. Focus on clarity and correctness over extreme optimization.