Easy to understand 100% Space and Time Solution
Intuition \uD83E\uDD14\nThe problem is to create a function that applies a function to each element of an array and returns a single value by reducing the array
# Intuition \uD83E\uDD14\nThe problem is to create a function that applies a function to each element of an array and returns a single value by reducing the array to a single value.\n\n# Approach \uD83D\uDE80\nThe approach used in the provided code is to create a reduce function that takes an array, a function to apply, and an initial value as arguments, and returns a single value. The function starts by setting the initial value to ans. It then iterates over each element of the input array and applies the provided function fn to accumulate the values into a single output.\n\nThis approach ensures that the function can be used to apply any function to an array and accumulate the output into a single value.\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\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    let ans = init;\n    for (let i of nums)\n        ans = fn(ans, i);\n    \n    return ans;\n};\n```
['JavaScript']