DAY 6 of JS challenge!⏳Easy solution with explanation!🚀Beginner friendly!📈
\n# Approach\n1. To implement the reduce function, we can iterate over each element of the array using a for loop\n2. apply the reducer function to the current 
\n# Approach\n1. To implement the `reduce` function, we can iterate over each element of the array using a `for loop`\n2. apply the reducer function to the current value and the current element and store it as `val`. \n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\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; //set current value\n    for(let i=0;i<nums.length;i++){ //iterate over each ele in array\n        val = fn(val, nums[i]);//store the val as function of the current val with the element\n    }\n    return val;//output\n};\n```\nKindly upvote if you found this helpful!Happy learning!
['JavaScript']