# Spot the bug - #19

**URL:** <https://forum.kirupa.com/t/spot-the-bug-19/680987>\
**Category:** web dev\
**Created:** [April 29, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-19/680987 "2026-04-29T07:00:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArthurDent](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/arthurdent/32/31262_2.png) [@ArthurDent](https://forum.kirupa.com/u/ArthurDent)\
**Post date:** [April 29, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-19/680987/1 "2026-04-29T07:00:08Z")

</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.

Arthur

---

<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:** [April 29, 2026, 8:28am UTC](https://forum.kirupa.com/t/spot-the-bug-19/680987/2 "2026-04-29T08:28:38Z")

</div>

In your snippet the reducer has braces but doesn’t `return` anything, so `total` ends up wrong (you’ll get `0` here because you passed an initial value of `0`) — did you mean to return `sum + p` from the callback? I might be wrong here.

---

<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:** [April 30, 2026, 2:07am UTC](https://forum.kirupa.com/t/spot-the-bug-19/680987/3 "2026-04-30T02:07:13Z")

</div>

yep, with braces you need an explicit `return` or the reducer hands back `undefined` and the total goes sideways.

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

```

---

<div class="post-metadata">

**Author:** ![VaultBoy](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/vaultboy/32/31832_2.png) [@VaultBoy](https://forum.kirupa.com/u/VaultBoy)\
**Post date:** [April 30, 2026, 5:49am UTC](https://forum.kirupa.com/t/spot-the-bug-19/680987/4 "2026-04-30T05:49:28Z")

</div>

Yeah, this is one of those arrow-function footguns: once you add `{}` you’ve got a block body, so you need an explicit `return` or your reducer returns `undefined` and the total goes off the rails.

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

```

Or just skip the braces and let it implicitly return:

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

```

I found a related kirupa. com article that can help you go deeper into this topic.
