Using Array.Map()
IntuitionFirst, I used an if statement to return the init value if the length of the nums array is 0 (an empty array). Then, I used the nums.map() method to ite
# Intuition
First, I used an `if` statement to return the `init` value if the length of the nums array is `0` (an empty array). Then, I used the `nums.map()` method to iterate over every element inside the `nums` array. For each element, I applied the provided reducer function `fn` and stored the returned value in a variable called `val`. Since in the next computation the `init` should be equal to the last computation result, I set `init = val`. Then, I returned the result of the map under `val`. The `map()` method should return a new array `newArray` with all the computation results. Since the final result will be at the end of the array, I retrieved it by returning the last element of the array using `newArray[newArray.length - 1]`.

Kindly, if you like my answer and explanation, please upvote me. 😊


# - Time complexity:
**Combining these steps, the overall time complexity is O(n).**

1- Empty Check:
The `if` statement to check if `nums.length` is `0` has a constant time complexity, O(1).

2- Map Method:
The `nums.map()` method iterates over every element of the array, so its time complexity is O(n), where `n` is the length of the `nums` array.

3- Reducer Function:
The function `fn` is applied to each element in the array. Assuming the function `fn` operates in constant time, O(1), the total time complexity is O(n).

# - Space complexity:
**Combining these steps, the overall space complexity is O(n).**

1- Empty Check: 
The space complexity of the `if` statement is O(1).

2- Map Method: 
The `nums.map()` method creates a new array, newArray, with the same length as the `nums` array. Therefore, the space complexity is O(n).

3- Intermediate Values: 
The variable `val` stores intermediate computation results, which takes O(1) space.

# 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
    }
    newArray = nums.map(
        (num)=>{
            val = fn(init,num);
            init = val
            return val
        }
    )
    return newArray[newArray.length - 1]
};
```
['JavaScript']