JavaScript Clean Simple || 2 Solutions With Detail Explanation
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
# 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 */\n// var reduce = function (nums, fn, init) {\n//     // edge case\n//     if (nums.length === 0) {\n//         return init;\n//     }\n\n//     // solve it\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\nvar reduce = function (nums, fn, init) {\n    let res = init\n    nums.forEach((num)=>{\n        res = fn(res, num)\n    })\n    return res\n}\n```
['Array', 'JavaScript']