JS Built-ins
TypeScriptintermediate

Generics β€” reduce<T, U>

πŸ“– Usage Example
reduce([1, 2, 3, 4], (acc, x) => acc + x, 0)   // β†’ 10

reduce<T, U> folds an array into a single value using an accumulator function.

Generics: reduce<T, U>

Challenge: Implement reduce<T, U>(arr: T[], fn: (acc: U, x: T) => U, init: U): U.

reduce([1,2,3,4], (acc, x) => acc + x, 0)  // β†’ 10
πŸ’‘ Hints (1)
  • β€’ Delegate to Array.prototype.reduce.