Intermediate Algorithm Scripting: Seek and Destroy_Not passing 3rd and 4th tests

Hello. Could someone tell me why the following code doesn’t pass the third and fourth tests?

destroyer([3, 5, 1, 2, 2], 2, 3, 5) should return [1].
destroyer([2, 3, 2, 3], 2, 3) should return [].

function destroyer(arr) {
  let args = Array.from(arguments).slice(1);
  for (let i = 0; i < args.length; i++) {
    for (let j = 0; j < arr.length; j++) {
      if (args[i] === arr[j]) {
        arr.splice(j,1);
      }
    }
  }
  return arr;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

For the 3rd test, your solution is returning [1, 2]

For the 4th test, your solution is returning [3]

Why? Because when you splice out 1 item, your j variable which is keeping track of the current index keeps incrementing, so certain elements are not evaluated by your if statement.

1 Like