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

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

#1

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);

#2

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.


#3

In the future, would you mind using the Ask for help button located on the challenge? It allows us to quickly locate the challenge and test out your code instead of having to search for the url and then copy/paste your code in to test it out.

Thanks.