Custom Reduce Function
IntuitionThe problem requires us to implement a custom reduce function that mimics the behavior of the built-in Array.prototype.reduce. The key idea is to itera
# Intuition
The problem requires us to implement a custom reduce function that mimics the behavior of the built-in Array.prototype.reduce. The key idea is to iteratively apply a callback function (fn) to an accumulator (init) and each element of the array, updating the accumulator at every step. If the input array is empty, the function should simply return the initial value (init).

# Approach
1. Check if the input array nums is empty:
    If nums.length === 0, directly return the initial value (init).
2. Iterate through the elements of the nums array using a for loop.
3. In each iteration, update the init value by applying the callback function (fn) with the current accumulator (init) and the current element (nums[i]).
4. After the loop completes, return the final value of the accumulator (init).

This approach simulates the behavior of the built-in reduce function without relying on built-in methods.

# Complexity
- Time complexity:
O(n) - Where n is the number of elements in the array. We iterate through the array once.

- Space complexity:
O(1) - No additional space is used apart from a few variables to store the accumulator and loop index.

# Code
```javascript []
/**
 * @param {number[]} nums
 * @param {Function} fn
 * @param {number} init
 * @return {number}
 */
var reduce = function(nums, fn, init) {
    if (nums.length === 0) return init;
    for (let i=0; i<nums.length; i++) {
        init = fn(init, nums[i]);
    }
    return init;
};
```
['JavaScript']