why is one of the same items in tandem in an array removed automatically? [duplicate]

Purpose: The function’s purpose is to remove all instances of val in the array nums in-place and return the new length of the array.

let nums= [0, 1, 2, 2, 2, 3, 0, 4, 2];
let val = 2;

I have done this task by filter() method easily, but I’m faced with a problem when I would want to solve with a for loop.

This problem only occurs when we have at least two same items in tandem in an array.
f we have two same items in tandem => only one item is logged. If we have three same items in tandem => only two items is logged

One of the similar item is always deducted in for loop!! why??

I expected : array = [0, 1, 3, 0, 4] => array.length = 5

But I got : array = [0, 1, 2, 3, 0, 4] => array.length = 6

function removeElement(array, val) {
  for (let i = 0; i < array.length; i++) {
    console.log(" array[i]:", array[i]); //only two of three items in tandem with value 2 is counted!!
    if (array[i] === val) {
      array.splice(i, 1);
    }
  }
  return array.length;
}

let nums = [0, 1, 2, 2, 2, 3, 0, 4, 2];
let val = 2;
const numsLength = removeElement(nums, val);
console.log("numsLength:", numsLength);  // The answer must be 5, but the functions gives 6