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.
The code that you posted at the top first checks if the value 2 (which is arguments[1]) is NOT in arr. It is in arr, so, you do nothing. Next, it checks if the value 3 (which is arguments[2]) is NOT in arr. It is is arr, so you do nothing.
After that, you have iterated through the arguments 1 through arguments.length - 1, so you just return newArr. You never push any value to newArr, so it does not contain any elements.
Instead, you should be iterating through arr and checking to see if each element in arr matches one of the other arguments after arr. If it does, then you push the arr element to newArr.
Hint: Think about how you could easily create an array of the arguments after the first argument arr to be able to still use the `includes method.