Skip to content

2626. Array Reduce Transformation 👍

1
2
3
4
5
6
7
8
9
type Fn = (accum: number, curr: number) => number;

function reduce(nums: number[], fn: Fn, init: number): number {
  let ans = init;
  for (const num of nums) {
    ans = fn(ans, num);
  }
  return ans;
}