$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · medium · #212

Which fix removes the bug?

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