Working on a challenge from freeCodeCamp, where I have to create a function that takes in an array and a number of other arguments. If an element within the passed array matches one of the passed arguments, it has to be removed from the array.
I have written this code, but it does not work for all of the tests:
function destroyer(arr) {
var args = Array.prototype.slice.call(arguments);
for(i=0; i<arr.length; i++){
for(var j=0; j<args.length; j++){
if(args[j]===arr[i]){
arr.splice(i,1);
}
}
}
return arr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
Step 1: Convert arguments object into an array, so I can treat it like one.
Step 2: Run nested for-loop to find any matches between the passed array and the passed array of arguments.
Step 3: If a match is found, eliminate it using .splice(i,1).
Shouldn't that do the trick? What am I doing wrong?
EDIT: This is an example of a test that it fails:
destroyer([3, 5, 1, 2, 2], 2, 3, 5) should return [1].