Seek and Destroy logical error

Tell us what’s happening:
Can you take a look on my code for this project? i got 3 out 5 tests correct, it doesn’t make sense with others two. Can anybody explain?

Your code so far

function destroyer(arr) {
  // Remove all the values
  for(i=1;i<arguments.length;i++){
    for(j=0;j<arr.length;j++){
      if(arguments[i]==arr[j]){
        arr.splice(j,1);  
      }
    }
  }
  return arr;//arguments[0];
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/seek-and-destroy

You always have to be careful when you are changing an array as you are indexing through it. You are splicing out a member and then going onto the next member. But then j gets incremented and then you’ve skipped over an element in the array. You need to decrement j whenever you splice so you don’t miss any.

1 Like

Thank you Kevin! it works as well.