# Spot the bug - #122: Leaderboard Table

**URL:** <https://forum.kirupa.com/t/spot-the-bug-122-leaderboard-table/683097>\
**Category:** web dev\
**Created:** [August 20, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-122-leaderboard-table/683097 "2026-08-20T07:00:08Z")\
**Posts on this page:** 3\
**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:** [August 20, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-122-leaderboard-table/683097/1 "2026-08-20T07:00:08Z")

</div>

Why is my inventory ranker mutating the original list?

```js
const potions = [{ name: "Elixir", power: 45 }, { name: "Brew", power: 90 }];

function getTopPotion(list) {
  const sorted = list.sort((a, b) => b.power - a.power);
  return sorted[0];
}

const best = getTopPotion(potions);
console.log(potions[0].name === "Elixir");

```

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

---

<div class="post-metadata">

**Author:** ![sora](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sora/32/31259_2.png) [@sora](https://forum.kirupa.com/u/sora)\
**Post date:** [August 21, 2026, 7:20am UTC](https://forum.kirupa.com/t/spot-the-bug-122-leaderboard-table/683097/2 "2026-08-21T07:20:25Z")

</div>

The `sort` method changes the array it is called on. It does not return a new array.

You can make a copy of `list` first. Something like `[...list].sort(...)` would work to keep the original `potions` array untouched.

---

<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:** [August 21, 2026, 8:00am UTC](https://forum.kirupa.com/t/spot-the-bug-122-leaderboard-table/683097/3 "2026-08-21T08:00:14Z")

</div>

**Spot the Bug answer:** The Array.prototype.`sort()` method sorts the elements of an array in place and returns the reference to the same array, thus mutating the original list.

**The fix:**

```js
const sorted = [...list].sort((a, b) => b.power - a.power);

```

**Why:**  
When `list.sort()` is called, it directly modifies the `potions` array because `list` is a reference to `potions`. To prevent this, a shallow copy of the array should be made before sorting. Using the spread operator `[...list]` creates a new array instance, allowing the sort operation to occur on the copy without affecting the original `potions` array.

**First-answer leaderboard**

1. @kirupa - 5 (firsts) 🏆
2. @adnanahmed - 2 (firsts)
3. @emmawalter5 - 1 (first)
