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.
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 ?
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…
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.
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…