# 🚀 2626 — Array Reduce Transformation ## 🔹 Array.prototype.reduce() āϕ⧀? reduce() āĻšāϞ āϜāĻžāĻ­āĻžāĻ¸ā§āĻ•ā§āϰāĻŋāĻĒā§āĻŸā§‡āϰ āĻāĻ•āϟāĻŋ āĻŦāĻŋāĻ˛ā§āϟ-āχāύ āĻŽā§‡āĻĨāĻĄ āϝ⧇āϟāĻž āĻāĻ•āϟāĻŋ āĻ…ā§āϝāĻžāϰ⧇āϕ⧇ āĻāĻ•āĻ• āĻ­ā§āϝāĻžāϞ⧁āϤ⧇ (āϏāĻ‚āĻ–ā§āϝāĻž, āĻ…āĻŦāĻœā§‡āĻ•ā§āϟ, āĻ¸ā§āĻŸā§āϰāĻŋāĻ‚ āχāĻ¤ā§āϝāĻžāĻĻāĻŋ) “āϰāĻŋāĻĄāĻŋāωāĻ¸â€ āĻ•āϰ⧇āĨ¤ āĻāϟāĻŋ āĻĒā§āϰāϤāĻŋāϟāĻŋ āωāĻĒāĻžāĻĻāĻžāύ⧇āϰ āĻ“āĻĒāϰ āφāĻĒāύāĻžāϰ āĻĻ⧇āĻ“āϝāĻŧāĻž āĻāĻ•āϟāĻŋ āϰāĻŋāĻĄāĻŋāωāϏāĻžāϰ āĻĢāĻžāĻ‚āĻļāύ āϚāĻžāϞāĻžāϝāĻŧāĨ¤ āĻĒā§āϰāϤāĻŋāϟāĻŋ āϧāĻžāĻĒ⧇ āĻāĻ•āϟāĻŋ accumulator (āϏāĻžā§āϚāĻŋāϤ āĻŽāĻžāύ) āĻĨāĻžāϕ⧇ āϝ⧇āϟāĻŋ āφāĻĒāĻĄā§‡āϟ āĻšāϝāĻŧāĨ¤ ### 🔹 āϏāĻŋāύāĻŸā§āϝāĻžāĻ•ā§āϏ ```js arr.reduce((accumulator, currentValue, index, array) => { // āĻāĻ–āĻžāύ⧇ āφāĻĒāĻĄā§‡āĻŸā§‡āĻĄ accumulator āϰāĻŋāϟāĻžāĻ°ā§āύ āĻ•āϰāϤ⧇ āĻšāĻŦ⧇ }, initialValue); ``` - accumulator → āφāϗ⧇āϰ āϧāĻžāĻĒ⧇āϰ āĻĢāϞāĻžāĻĢāϞ (āĻŦāĻž āĻĒā§āϰāĻĨāĻŽā§‡ initialValue) - currentValue → āĻ…ā§āϝāĻžāϰ⧇āϰ āĻŦāĻ°ā§āϤāĻŽāĻžāύ āωāĻĒāĻžāĻĻāĻžāύ - index → āϐ āωāĻĒāĻžāĻĻāĻžāύ⧇āϰ āχāĻ¨ā§āĻĄā§‡āĻ•ā§āϏ (āϐāĻšā§āĻ›āĻŋāĻ•) - array → āĻŽā§‚āϞ āĻ…ā§āϝāĻžāϰ⧇ (āϐāĻšā§āĻ›āĻŋāĻ•) - initialValue → accumulator-āĻāϰ āĻļ⧁āϰ⧁āϰ āĻŽāĻžāύ ### 🔹 āϕ⧇āύ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻž āĻšāϝāĻŧ - āϏāĻ‚āĻ–ā§āϝāĻžāϗ⧁āϞ⧋āϰ āϝ⧋āĻ—āĻĢāϞ āĻŦāĻž āϗ⧁āĻŖāĻĢāϞ āĻŦ⧇āϰ āĻ•āϰāĻž - āĻ…āĻŦāĻœā§‡āĻ•ā§āϟ āĻŦāĻž āĻŽā§āϝāĻžāĻĒ āĻŦāĻžāύāĻžāύ⧋ (āϝ⧇āĻŽāύ āĻĢā§āϰāĻŋāϕ⧋āϝāĻŧ⧇āĻ¨ā§āϏāĻŋ āĻ•āĻžāωāĻ¨ā§āϟ) - nested āĻ…ā§āϝāĻžāϰ⧇ āĻĢā§āĻ˛ā§āϝāĻžāϟ āĻ•āϰāĻž - āĻāĻ•āχ āĻĒāĻžāϏ⧇ filter+map āĻ•āϰāĻž - āĻĄā§‡āϟāĻž āĻŸā§āϰāĻžāĻ¨ā§āϏāĻĢāϰāĻŽā§‡āĻļāύ ### 🔹 āωāĻĻāĻžāĻšāϰāĻŖ ā§§ – āϏāĻ‚āĻ–ā§āϝāĻžāϰ āϝ⧋āĻ—āĻĢāϞ ```js const numbers = [10, 20, 30, 40]; const sum = numbers.reduce((acc, n) => acc + n, 0); console.log(sum); // 100 ``` ### 🔹 āωāĻĻāĻžāĻšāϰāĻŖ ⧍ – āύ⧇āĻ¸ā§āĻŸā§‡āĻĄ āĻ…ā§āϝāĻžāϰ⧇ āĻĢā§āĻ˛ā§āϝāĻžāϟ āĻ•āϰāĻž ```js const nested = [[1,2],[3,4],[5]]; const flat = nested.reduce((acc, arr) => acc.concat(arr), []); console.log(flat); // [1,2,3,4,5] ``` ### 🔹 āωāĻĻāĻžāĻšāϰāĻŖ ā§Š – āĻĢā§āϰāĻŋāϕ⧋āϝāĻŧ⧇āĻ¨ā§āϏāĻŋ āĻŽā§āϝāĻžāĻĒ āĻŦāĻžāύāĻžāύ⧋ ```js const pets = ['cat','dog','cat','fish','dog']; const freq = pets.reduce((acc, pet) => { acc[pet] = (acc[pet] || 0) + 1; return acc; }, {}); console.log(freq); // { cat: 2, dog: 2, fish: 1 } ``` ## Problem Statement āφāĻŽāĻžāĻĻ⧇āϰ āĻāĻ•āϟāĻŋ `reduce` āĻĢāĻžāĻ‚āĻļāύ āϤ⧈āϰāĻŋ āĻ•āϰāϤ⧇ āĻšāĻŦ⧇, āϝ⧇āϟāĻŋ āĻāĻ•āϟāĻŋ āĻ…ā§āϝāĻžāϰ⧇ `arr`, āĻāĻ•āϟāĻŋ reducer function `fn` āĻāĻŦāĻ‚ āĻāĻ•āϟāĻŋ `initialValue` āύ⧇āĻŦ⧇āĨ¤ āĻāχ āĻĢāĻžāĻ‚āĻļāύāϟāĻŋ āĻĒā§āϰāϤāĻŋāϟāĻŋ āϧāĻžāĻĒ⧇ `fn(accumulator, currentValue, index)` āϚāĻžāϞāĻŋā§Ÿā§‡ āĻāĻ•āϟāĻŋ āĻāĻ•āĻ• āφāωāϟāĻĒ⧁āϟ āĻ­ā§āϝāĻžāϞ⧁ āϰāĻŋāϟāĻžāĻ°ā§āύ āĻ•āϰāĻŦ⧇āĨ¤ --- ## Approaches ### 🔹 1. Using a `for` loop (classic way) ```javascript var reduce = function (arr, fn, initialValue) { let acc = initialValue; for (let i = 0; i < arr.length; i++) { acc = fn(acc, arr[i], i); } return acc; }; ``` ### 🔹 2. Using for...of with index ```js var reduce = function (arr, fn, initialValue) { let acc = initialValue; for (let [i, val] of arr.entries()) { acc = fn(acc, val, i); } return acc; }; ``` ### 🔹 3. Using forEach ```js var reduce = function (arr, fn, initialValue) { let acc = initialValue; arr.forEach((val, i) => { acc = fn(acc, val, i); }); return acc; }; ``` ### 🔹 4. Using recursion ```js var reduce = function (arr, fn, initialValue, i = 0) { if (i >= arr.length) return initialValue; const nextAcc = fn(initialValue, arr[i], i); return reduce(arr, fn, nextAcc, i + 1); }; ``` ### Example Usage ```js // sum of numbers console.log( reduce([1, 2, 3, 4], (acc, n) => acc + n, 0) ); // 10 // concatenate strings console.log( reduce(['a','b','c'], (acc, ch) => acc + ch, '') ); // "abc" // flatten nested array console.log( reduce([[1,2],[3,4],[5]], (acc, arr) => acc.concat(arr), []) ); // [1,2,3,4,5] ``` ✅ All these implementations produce the same result. ## Key Takeaways * reduce āĻĻāĻŋā§Ÿā§‡ āϝ⧇āϕ⧋āύ⧋ āϧāϰāύ⧇āϰ āĻ…ā§āϝāĻžāĻ—ā§āϰāĻŋāϗ⧇āĻļāύ, āĻŸā§āϰāĻžāĻ¨ā§āϏāĻĢāϰāĻŽā§‡āĻļāύ, āĻŦāĻž āύāϤ⧁āύ āĻ¸ā§āĻŸā§āϰāĻžāĻ•āϚāĻžāϰ āϤ⧈āϰāĻŋ āĻ•āϰāĻž āϝāĻžā§ŸāĨ¤ * Classic loops, higher-order functions āĻāĻŦāĻ‚ recursion āϏāĻŦāĻ•â€™āϟāĻŋāχ āĻāĻ•āχ āĻĢāϞāĻžāĻĢāϞ āĻĻāĻŋāϤ⧇ āĻĒāĻžāϰ⧇āĨ¤ * āĻāχ āĻĒā§āϝāĻžāϟāĻžāĻ°ā§āύāϗ⧁āϞ⧋ āĻļāĻŋāĻ–āϞ⧇ functional programming āĻ“ array manipulation āĻ āĻĻāĻ•ā§āώāϤāĻž āĻŦāĻžā§œā§‡āĨ¤