Implementing custom reduce() function on LeetCode

Day 7 of #30DaysOfJavaScript on LeetCode Today's challenge: 2626 — Array Reduce Transformation The task was to implement a custom reduce() function that applies a reducer function (fn) on each element of an array, accumulating the result — just like the built-in Array.reduce() method, but without using it. Here’s my solution 👇 var reduce = function(nums, fn, init) { let ans = init; for (let i = 0; i < nums.length; i++) { ans = fn(ans, nums[i]); } return ans; }; This challenge helped me understand how accumulators work and how values are carried forward during iteration. It deepened my appreciation for how methods like reduce() process arrays step by step turning a list of elements into a single meaningful result. If you’d like to give it a shot, check it out here: https://lnkd.in/g6WC5mu7 #JavaScript #LeetCode #CodingChallenge #LearningJourney #30DaysOfCode #Functions #Callbacks #Arrays #Reduce #Programming

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories