JavaScript Solution - Array Reduce Transformation
IntuitionTo iterate each element in the given nums array and updating val on every iteration with the new value is returned by fn method.Approach
Create the new
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
To iterate each element in the given `nums` array and updating `val` on every iteration with the new value is returned by `fn` method.


# Approach
<!-- Describe your approach to solving the problem. -->
1. Create the new variable `val` and assign the `init` value into that variable.
2. Iterate the each element of the `nums` array using `for` loop.
3. Execute the `fn(val, nums[i])` and updating the return value into the existing variable `val`.
4. return the accumulated `val`

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

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