# Spot the bug - #168: Potion Inventory Grid

**URL:** <https://forum.kirupa.com/t/spot-the-bug-168-potion-inventory-grid/683616>\
**Category:** web dev\
**Created:** [October 8, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-168-potion-inventory-grid/683616 "2026-10-08T07:00:16Z")\
**Posts on this page:** 1\
**Page:** 1

<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:** [October 8, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-168-potion-inventory-grid/683616/1 "2026-10-08T07:00:16Z")

</div>

Why is this potion shelf shuffling the wrong bottle types?

```js
function organizePotionShelf(slots, incomingBatch) {
  const shelf = Array.from(slots);
  const uncataloged = [];

  for (const bottle of incomingBatch) {
    if (bottle.tier < 1 || bottle.tier > 5) {
      uncataloged.push(bottle);
      continue;
    }

    const targetIndex = shelf.findIndex(
      (slot) => slot && slot.tier === bottle.tier && !slot.sealed
    );

    if (targetIndex !== -1) {
      shelf.splice(targetIndex, 1, bottle);
    } else {
      const emptyIdx = shelf.indexOf(null);
      if (emptyIdx !== -1) {
        shelf[emptyIdx] = bottle;
      } else {
        uncataloged.push(bottle);
      }
    }
  }

  const denseShelf = shelf.filter(Boolean);
  const sortedByTier = denseShelf.toSorted((a, b) => a.tier - b.tier);

  const reallocated = sortedByTier.reduce((acc, bottle, idx, arr) => {
    if (idx === 0 || bottle.tier !== arr[idx - 1].tier) {
      acc.push([bottle]);
    } else {
      acc.at(-1).push(bottle);
    }
    return acc;
  }, []);

  const flattenedDisplay = reallocated.flatMap((group) => {
    const padded = new Array(3).fill(null);
    return padded.map((_, i) => group[i] ?? { placeholder: true });
  });

  return {
    slots: flattenedDisplay,
    overflowCount: uncataloged.length
  };
}

```

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