Easy Javascript Solution Using reduce method ✅✅
Intuition\n Describe your first thoughts on how to solve this problem. \n Since we have to reduce array to a single value , we will use reduce method of javascr
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n Since we have to reduce array to a single value , we will use reduce method of javascript.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n We will implement our own reduce method.\n   1. Initialize val=init\n ```\n   for(var v of nums)\n   {\n       val=fn(val,v);\n   }\n```\n  3. val is our result.\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 val=init;\n    for(var x of nums)\n    {\n        val=fn(val,x);\n    }\n    return val;\n};\n```
['TypeScript', 'JavaScript']