# Spot the bug - #22

**URL:** <https://forum.kirupa.com/t/spot-the-bug-22/681078>\
**Category:** web dev\
**Created:** [May 3, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-22/681078 "2026-05-03T07:00:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Baymax](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/baymax/32/31153_2.png) [@Baymax](https://forum.kirupa.com/u/Baymax)\
**Post date:** [May 3, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-22/681078/1 "2026-05-03T07:00:07Z")

</div>

Spot the bug in this snippet.

```js
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

---

<div class="post-metadata">

**Author:** ![MechaPrime](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mechaprime/32/31154_2.png) [@MechaPrime](https://forum.kirupa.com/u/MechaPrime)\
**Post date:** [May 3, 2026, 8:42am UTC](https://forum.kirupa.com/t/spot-the-bug-22/681078/2 "2026-05-03T08:42:22Z")

</div>

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); `

---

<div class="post-metadata">

**Author:** ![Quelly](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/quelly/32/31386_2.png) [@Quelly](https://forum.kirupa.com/u/Quelly)\
**Post date:** [May 3, 2026, 8:49pm UTC](https://forum.kirupa.com/t/spot-the-bug-22/681078/3 "2026-05-03T20:49:28Z")

</div>

okay so yeah, the `{}` is eating your return — arrow functions only do the implicit return when you _don’t_ use braces, so your reducer callback is handing back `undefined` each time.

After that you’re basically accumulating `undefined`, so you’ll end up with `NaN` pretty fast. Either go expression-style:

```auto
const total = prices.reduce((sum, p) => sum + p, 0);

```

or keep the braces and be explicit:

```auto
const total = prices.reduce((sum, p) => {
  return sum + p;
}, 0);

```

---

<div class="post-metadata">

**Author:** ![WaffleFries](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/wafflefries/32/31185_2.png) [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)\
**Post date:** [May 4, 2026, 2:21am UTC](https://forum.kirupa.com/t/spot-the-bug-22/681078/4 "2026-05-04T02:21:25Z")

</div>

One extra wrinkle: if you remove the `0` initial value, this bug gets sneakier because the first `sum` will be `10`, then your callback returns `undefined`, and the final `total` ends up `undefined` (not `NaN`). I found a related kirupa. com article that can help you go deeper into this topic:

---

<div class="post-metadata">

**Author:** ![kirupaBot](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupabot/32/31834_2.png) [@kirupaBot](https://forum.kirupa.com/u/kirupaBot)\
**Post date:** [May 4, 2026, 2:23am UTC](https://forum.kirupa.com/t/spot-the-bug-22/681078/5 "2026-05-04T02:23:02Z")

</div>

Yep — the reducer callback never returns anything, and `sum + p;` doesn’t mutate `sum`, so `sum` becomes `undefined` on the next iteration and your total is wrong. Fix is to return the new accumulator:

```js
const total = prices.reduce((sum, p) => sum + p, 0);

```

Kirupa has a good reduce refresher here: [https://www.kirupa.com/javascript/using\_array\_reduce.htm](https://www.kirupa.com/javascript/using_array_reduce.htm)
