Why "not includes" doesn't work?

Tell us what’s happening:

I’m trying to solve this using a for loop so I can iterate through the args and solve the problem of having more than 3 args in.

Using .includes on my if statement is returning the elements in the arr found in the other args, however, when I negate that, I’m getting the whole arr. Why is that?

Your code so far


function destroyer(arr) {
let newArr = []
for (let i = 1; i < arguments.length; i++){
    if (!arr.includes(arguments[i])) {
    newArr.push(arguments[i])
    }
}
return newArr
//arr.filter(item => (item !== arguments[1] && item !== arguments[2]))
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36.

Challenge: Seek and Destroy

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

Take a look at what is in arguments.

arguments[0] = the arr
Which I corrected my loop to star counting at 1

arguments[1] =2
arguments[2] =3

I not sure what you mean… This doesn’t explain why negating the if statement is not giving me the inverse.

You forgot arguments[0].

I’m still getting an empty array. It’s like my push() is not doing anything.

what is your algorithm doing? tell us in your own words.

loop trough the args length,
if the arr doesn’t include that current iteration of arguments[i])
push the value of i into the newArr

and what is your desired result?

to get a new arr with the values that are not included in the the args[2] and args[3]

and what are you adding to this new array instead?

Nothing. The arr in not begin passed in. Therefore I cannot obtain [1], because im not looping over the first arg… Gotcha. Now I need to find another solution. In my mind I thought that negating the if statement was enough to do the trick. Can you provide some extra ideas ?

first we need to understand what is going wrong, it doesn’t seem you have that clear
what is it that is being pushed to newArr?

for this to work I would need to change the original array, by poping all the values out referred by the second and third args. Although I don’t see how can I do that…

what is begin pushed is 2 and 3. Arg[1] and arg[2].

and why are you pushing those if you want to keep only numbers that are different from that?

Because I tried to make it work like that in the first place, and thought that by negating that would work. I didn’t!

you are inverting when the if statement is executed, not what is being pushed to newArr. If you don’t want those numbers you can’t push them to newArr.

anyway, so, you want to keep some values of arr , right?
then you may want to check each value of arr individually instead of with includes. And then do or not do something with that specific value of arr, depending on something.

That I already did:

arr.filter(item => (item !== arguments[1] && item !== arguments[2]))

But that I ran into trouble because the amount of args. Thats why I abandoned that strategy.

I believe what you are saying is to create a new array made from [ arg[1], arg[2] ] and then compare if the values of arg[0] are in the new arr. But I still can’t see how I go around variable args…

Im pretty sure that the solution is very simple, and I’m complicating it because I;m getting frustrated…