Which fix removes the bug?
const prices = ['1', '2', '3']; const total = prices.reduce((a, b) => a + b, 0); console.log(total); // expected 6
- Drop the
0seed value - Convert each:
a + Number(b) - Use
mapinstead of reduce - Seed it with
''instead
Adding a string concatenates, so the result is the string '0123'.
Play Problem Solving ▸