Spot the bug - #22

Spot the bug in this snippet.

const prices = [10, 20, 30];
const total = prices.reduce((sum, p) => {
  sum + p;
}, 0);

console.log(total);

Reply with what is broken and how you would fix it.

BayMax

Your reducer callback uses {} but never returns anything, so the accumulator becomes undefined and you end up with the initial value (or undefined depending on the setup) instead of the sum. Fix it by returning the expression: const total = prices. reduce((sum, p) => sum + p, 0); or, if you want braces: const total = prices. reduce((sum, p) => { return sum + p; }, 0);