Beats 83.9%✔ | Beginner friendly Brute Force Solution
IntuitionWe are asked to implement the working of arrays.reduce() method in javascript which is a higher-order function that takes an array, a reducer function,
# Intuition
We are asked to implement the working of arrays.reduce() method in javascript which is a higher-order function that takes an array, a reducer function, and an initial value, and returns a single accumulated value by applying the reducer function to each element of the array.

# Approach
Firstly, I analyzed the problem and implemented what was asked i.e., an explicit check for length of the array and used for loop and using the ternary operator checked the condition and returned the value.
In the second solution, trying to refactor the code, I came up with a for...of loop solution and explored that the length condition mentioned in the question was implicitly being handled.

# Complexity
- Time complexity: The time complexity of the reduce function implementation is O(n), where n is the length of the array, because the function iterates over each element of the array exactly once.

- Space complexity: The time complexity of the reduce function implementation is O(1), because the function only uses a single accumulator variable (val) and does not allocate any extra space proportional to the input size.

# Code
```javascript []
/**
 * @param {number[]} nums
 * @param {Function} fn
 * @param {number} init
 * @return {number}
 */
var reduce = function(nums, fn, init) {
    // 1st Solution
    // if(nums.length===0) return init;
    // let temp=0;
    // for(let i=0;i<nums.length;i++) i==0?temp=fn(init,nums[i]):temp=fn(temp,nums[i]);
    // return temp;
    // 2nd Using for...of loop
    let val = init;
    for (const element of nums) {
        val = fn(val,element);
    }
    return val;
};
```
Please, Upvote and share your thoughts. Happy Coding..!💗
['JavaScript']