You should not use splice on an array that you’re attempting to loop over because splice changes mutates it in place. Basically on an iteration where you remove a value at [i], the following value that was once at [i + 1] is now at [i]. But the loop has already ended that iteration, so it will skip that value.
My hint for you is, instead of removing items from the array given to you, instead try to build a new array up from nothing that only includes the correct values.