JS Built-ins
FunctionalProgrammingintermediate

Custom reduce β€” transform array

πŸ“– Usage Example
function myReduce(arr, fn, init) {
  let acc = init;
  for (const item of arr) acc = fn(acc, item);
  return acc;
}
myReduce([1, 2, 3, 4], (sum, x) => sum + x, 0); // 10

Reduce accumulates a result by applying a combining function to each element.

Reduce for Accumulation

Reduce transforms an array into a single value by accumulating results.

Challenge: Implement myReduce that sums all elements in an array (using Array.prototype.reduce internally).

πŸ’‘ Hints (3)
  • β€’ myReduce is a wrapper around Array.prototype.reduce
  • β€’ Pass fn and initial to reduce
  • β€’ Reduce accumulates values into a single result