Simple code :stom Array Reduction Function in JavaScript"
Intuition\nThe goal of this function is to reduce an array of numbers by applying a given reduction function to each element sequentially, starting with an init
# Intuition\nThe goal of this function is to reduce an array of numbers by applying a given reduction function to each element sequentially, starting with an initial value. This is essentially a custom implementation of a reduce operation similar to the built-in Array.reduce method in JavaScript.\n\n# Approach\n1 .First, the function checks if the input array nums is empty. If it is, it returns the initial value init because there are no elements to process.\n\n2 .If the array is not empty, it initializes an accumulator variable with the initial value init.\n\n3 .Then, it iterates through the array using a for loop, starting from the first element to the last.\n\n4 .During each iteration, it applies the provided reduction function fn to the current value of the accumulator and the current element of the array (nums[i]), storing the result back in the accumulator.\n\n5 .After processing all elements in the array, the final value of the accumulator represents the result of the reduction operation, and this value is returned.\n\n# Complexity\n- Time complexity:\n O(n)\n- Space complexity:\nO(1)\n# Code\n```\n/**\n * @param {number[]} nums\n * @param {Function} fn\n * @param {number} init\n * @return {number}\n */\nvar reduce = function(nums, fn, init) {\n\n    if(nums.length === 0){\n        return init;\n    }\n\n\n    let accumulator = init\n    for(let  i = 0;i < nums.length;i++){\n    accumulator = fn(accumulator, nums[i])\n    }\n    return accumulator;\n};\n```
['JavaScript']