🔥🚀 4 Easy solution with Video Explanation in JS & TS 🔥 | Learn concepts first ✅
\n# Video explanation \nVideo link\n\n___\n\n\n## Intution - steps to think (explained in video too)\n The problem is asking us to return a reduced array \n we 
\n# Video explanation \n[Video link](https://youtu.be/10iEbeg30mE)\n\n___\n\n\n## Intution - steps to think (explained in video too)\n* The problem is asking us to return a reduced array \n* we will start by declaring the variable and return it\n* between declaration and return, we will loop over the array and try to apply the given function to it.\n\n\n---\n\n\n# Complexity\n\n### Time complexity:\nThe time complexity of the above solution is O(n), where n is the length of the input array arr.\n\n### Space complexity:\nThe space complexity of the solution is O(1) as well. This is because we are just using the variable to store the result, nothing else.\n\n# Code\n\n\n```javascript []\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 res = init;\n    for (let i = 0; i < nums.length; i++) {\n        res = fn(res, nums[i])\n    }\n    return res\n};\n```\n```typescript []\nconst reduce = function(nums: number[], fn: (acc: number, curr: number) => number, init: number): number {\n    let res: number = init;\n    for (let i = 0; i < nums.length; i++) {\n        res = fn(res, nums[i])\n    }\n    return res\n};\n```\n\n```javascript []\n\n\n/*Solution 2*/\n var reduce = function (nums, fn, init) {\n   let res = init;\n    nums.map(x => (res = fn(res, x)));\n\n    return res;\n};\n\n/* Solution 3 */\n// var reduce = function (nums, fn, init) {\n//    let res = init;\n//    nums.forEach(x => (res = fn(res, x)));\n\n//    return res;\n// };\n\n/*Solution 4*/\n// var 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\n\n```\n\n\nOther good resources:-\n* [My video explanation]()\n* [Loops in JS](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration)\n* [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)\n* [Filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)\n* [Reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce)\n\n\n# Important links\n* [Join 100+ other JS lovers](https://discord.gg/2BxFN63EFc) \uD83D\uDE80\n* [Reach out to me on Linkedin.](https://www.linkedin.com/in/anshulontech/) \uD83D\uDE4F\uD83C\uDFFB\n\n\n\n![upvote me.jpeg](https://assets.leetcode.com/users/images/bc2afe3a-768d-42cb-aba8-39023ef69af2_1683593182.9982169.jpeg)\n
['JavaScript']