Can someone clear my little confusion here regarding array.filter please


#1

Hi I am doing this challenge and kind of got stumped over a point that why is the below code not working.

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

return arr.filter(function(val){return val!=arguments[2];});
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3); //why doesn’t it return 1,2,1,2 ?

I haven’t yet gotten to the point of iterating through the arguments to filter out all the matches from our arr (array). (but thats not my question) I am only asking that as you can see that i am applying filter method on array (arr) and only trying to filter out values using a comparison against the value of arguments[2]. why is it not working? because when i delete this all and just run return arguments[2]; then that returns the correct value so why not working in my code then? Please help someone :slight_smile:


#2

I think I see the problem, you need to store the arguments[2] in a variable in the destroyer function. otherwise its looking for additional arguments given to the function inside of arr.filter, which doesn’t have another argument passed to it. The following works

function destroyer(arr) {
// Remove all the values
var args=arguments

return arr.filter(function(val){return val!=args[2];});

}

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

Good Luck!


#3

Thanks a lot. Much appreciated!