3 Different Method || Beginner Friendly || Day 6 || Explained
Using Built in reduce\nIn this method we use built in reduce() which is provided by javascript.\n\n# Code\n\nvar reduce = function (nums, fn, init) {\n  return 
# Using Built in reduce\nIn this method we use built in ```reduce()``` which is provided by javascript.\n\n# Code\n```\nvar reduce = function (nums, fn, init) {\n  return nums.reduce(fn,init);\n};\n```\n\n# Using For Loop\nIn this solution we use conventional for loop to iterate each element of array. Firstly we take ans variable ans and assign it to inital value then we itreate over array using for loop and pass ans and element of array to function ```ans = fn(ans,nums[i])``` .\n\n# Code\n```\nvar reduce = function (nums, fn, init) {\n  let ans = init;\n  for (let i = 0; i < nums.length; i++) {\n    ans = fn(ans, nums[i]);\n  }\n  return ans;\n};\n```\n\n# Using forEach \n\nIn this we use builtin ```forEach()``` which is provided by javascript.\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\n    nums.forEach((n) => {\n        ans = fn(ans,n);\n    });\n\n    return ans;\n};\n```\n![478xve.jpg](https://assets.leetcode.com/users/images/996b2059-933d-42cd-bf7e-820cc4ba5e91_1683701398.1942718.jpeg)\n
['JavaScript']