Seek and Destroy //indexOf()not working

Tell us what’s happening:

sorry i cant seem to debug this i read the documentation it should work fine but it doesnt…? unsure why

Your code so far


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

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

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/seek-and-destroy

You don’t say what is happening, but I think if (test.indexOf(arr[i])) is your problem. When indexOf() doesn’t find the value it returns -1, and -1 is a ‘truthy’ value (0 is false).

arr is just [1, 2, 3, 1, 2, 3], if you want to reach the other arguments you need to do in a different way. for example the arguments object, or using the rest operator. indexOf() is an array method but you are actually using it on a number, so yout code throws an error and stop working.

I suggest using a tool like http://pythontutor.com/javascript.html to see what your code does