Why doesn't this work

Tell us what’s happening:

Your code so far


function destroyer(arr) {
var args = Array.prototype.slice.call(arguments);

for(var i=0;i<arr.length;i++){
  for (var j=0;j<args.length;j++){
    if(arr[i]===args[j]){
      arr.splice(i,1);
    }
  }
}
return arr;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);
console.log(destroyer(["possum", "trollo", 12, "safari", "hotdog", 92, 65, "grandma", "bugati", "trojan", "yacht"], "yacht", "possum", "trollo", "safari", "hotdog", "grandma", "bugati", "trojan"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0.

Challenge: Seek and Destroy

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

you are changing the array on which you are iterating
so there are some elements not checked
don’t use splice here, there are other ways

also, args[0] is arr you don’t need to check that too

I would try to use the rest operator to store the elements to check in a second array, instead of using the arguments object - it may be more elegant but also more intuitive in its implementation

1 Like

Thanks for that. I understand now