Seek and Destroy bug

In this challenge, they told me to do this:
You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.

This is my code:
function destroyer(arr) {
// Remove all the values

for(var i=1; i<arguments.length; i++){
arr= arr.filter(function(value){
return value !== this;
},arguments[i]);
}
return arr;
}

It’s returning the correct values for the diferent call examples but FreeCodeCamp isn’t giving me the check marks to complete the challenge. What am I doing wrong?

Hello

This is tricky :slight_smile:

Basically, the issue is that the value parameter is a primitive and the this parameter has been coerced into an object. (I think that the filter method must have done this). So, when performing a strict equality comparison (===), the types do not match and so they are not equal.

** You can check this by using console.log(typeof value) and console.log(typeof this) and then checking the console window

This can be quickly resolved by changing the comparison to (==) to allow for coercion or changing the expression to value !== this.valueOf() which will do a similar thing.

I would recommend a few things. Its generally not a good practice to reassign your function arguments (arr in this case). Rather slice() the array into a new variable if you need to reassign it. Also, you can avoid the for loop altogether by converting the arguments object into an array (see the MDN) and then using the indexOf method to determine if a value is in the arguments list.

I’m still looking into why the result returned is the correct one as shown in the results window. The code above does not perform any filtering on the array and so it should return the first argument as is. Using logging statements, it seems that this is the case, but for some reason, the correct answer is used as the input to the function for the 2nd round.

You can verify this with the following code:

function destroyer(arr) {
// Remove all the values

for(var i=1; i<arguments.length; i++){
arr= arr.filter(function(value){
return value !== this; 
},arguments[i]);
}
console.log('------');
  console.log(arr);
console.log('------');
return arr;
}


destroyer(["tree", "hamburger", 53], "tree", 53);
1 Like

Thanks for the solution, it worked! Sorry for the delay, the uni got hard the past months. But now I’m back in business :+1: