Spot the Bug answer: The loop does not correctly iterate over the list after an item is removed.
The fix:
Decrement the loop counter ‘i’ after splicing an item: ‘i–;’.
Why:
When an item is removed using ‘splice’, the array length decreases and subsequent elements shift their indices. If ‘i’ is not decremented, the next element in the original sequence is skipped because the loop counter advances past its new position.
The backward iteration approach, while functionally correct for mutating arrays in place, introduces a slightly higher cognitive load for maintainability compared to filter(). I found a related kirupa.com article that can help you go deeper into this topic: Removing Elements From Array