You’re changing the size of the array as you loop over it, removing elements with splice. Say the array is length 4, so the code in the loop runs 4 times. When you’ve done 2 iterations, you’ve removed 2 elements, so the array is only length 2. But your loop is set up to keep going, end though there’s now nothing there
Just don’t use splice, use something that doesn’t alter the array you’re iterating over (hint: there is a method that does exactly this with one letter different to the one you’re using)