What is the correct fix?
const ok = [1, 2, 3].reduce((a, b) => a + b); const bad = [].reduce((a, b) => a + b);
- Give a seed:
.reduce((a, b) => a + b, 0) - Give a seed:
.reduce(0, (a, b) => a + b) - Guard with
if (arr.length > 1)first - Change
reducetoreduceRighthere
reduce on an empty array without an initial value throws a TypeError.
Play Problem Solving ▸