I’m stuck on the Search and Destroy challenge.
I’ve found a solution which seems to return the correct result for each of the input values tested for, yet each one checks out as wrong when I try to run it.
Can anyone help me out?
This is my code:
function destroyer(arr) {
// Remove all the values
arrNew = Array.from(arguments[0]);
for(i = 1; i < arguments.length; i++){
arrNew = arrNew.filter(remove, arguments[i]);
}
return arrNew;
}
function remove(val) {
return val !== this;
}
//destroyer([1, 2, 3, 1, 2, 3], 2, 3);
destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3);
//destroyer([3, 5, 1, 2, 2], 2, 3, 5);
//destroyer([2, 3, 2, 3], 2, 3);
//destroyer(["tree", "hamburger", 53], "tree", 53);
If you ran it on repl.it, you’ll see that the output is not correct.
1 Like
True - thanks. I was not familiar with that site.
freeCodeCamp does give the right output though.
Also, I am kind of stumped as to why it doesn’t work
If I understood it right, arguments[i]
is used as the value of this
, but it is converted first into an object, so you’re actually comparing val
with an object with !==
. As far as I know, if you compared an abject against any non-object (val
) with !==
, you’ll always get true
.
Thank - I’ll look into that.
So, != instead of !== solved it - thanks for the pointer.
Still not sure why fCC gave me the right output, when the code was wrong. Gonna have to see if I can find a good place bring that to the attention of someone who might be able to fix it.
… and found the GitHub thread already there. And you already in it @kevcomedia . Hope someone finds a way to solve the output, but till then, once again thanks for the help.
@einblock In your .filter()
callback you just passed this
and here is the problem, because filter()
thisArg
only accepts array
/ object
, in that case, pass [arguments[i]]
instead of just arguments[i]
, and within your callback use this[0]
instead of this
. already tested it, it will surely works.
TC.