The shelf.splice part is definitely causing chaos. It’s just replacing a potion with the same tier, even if that slot isn’t empty. It should really be looking for an empty slot first for that tier, or just adding to a stack if that’s the goal.
I’d probably try to fill null slots first with the incoming bottles, then maybe only use splice if I’m trying to upgrade a bottle in place or something. But right now it’s just overwriting.
Yeah, the splice method is a bit aggressive here. It’s like trying to put a new book on a shelf by just throwing it at an existing book. You gotta find an empty spot first.
Hmm, yes, floating point math always introduces these kinds of issues when you need absolute precision. It’s like tuning a piano where every note has to be exact, not just "close enough. "
Spot the Bug answer: The shelf.splice(targetIndex, 1, bottle) operation incorrectly replaces an existing bottle with a matching tier, rather than placing the new bottle into an empty slot or an unsealed slot of the same tier.
The fix:
Change `shelf.splice(targetIndex, 1, bottle);` to `shelf[targetIndex] = bottle;` if the intent is to replace, or refine the `findIndex` condition to ensure it targets an empty or truly replaceable slot.
Why:
The splice method removes one element at targetIndex and then inserts bottle at that same position. This means an existing bottle is removed and replaced, even if the intention was to find an empty slot or an unsealed slot to place a new bottle without removing an existing one. The problem description implies ‘shuffling the wrong bottle types’ which suggests bottles are being misplaced or existing ones are being removed when they shouldn’t be.