Seek and Destroy...I'm not sure what I'm missing

Tell us what’s happening:
Describe your issue in detail here.
My idea was to loop through arr and compare each of the elements to one of the outside arguments and push those that match the outside elements into a separate array and return that array.
Yet…my algorithm keeps taking in everything…what am I overlooking?

  **Your code so far**

function destroyer(arr) {
let array = [];
for(let i = 0; i < arr.length; i++){
  if(arr[i]===arguments[1] || arguments[2]){array.push(arr[i])}
 console.log(array)
}
return arr;
}

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/96.0.4664.110 Safari/537.36

Challenge: Seek and Destroy

Link to the challenge:

In your if, what it looks like you’re trying to do is check if arr[i] is equal to the second or third argument, but what you’re actually doing is checking if it’s equal to the second argument or the third argument is truthy.

I’m trying filter() and I’ve converted arguments to an array but still no success. Would you mind taking a look below and telling me where my logic falters?

Thank you, Nick

function destroyer(arr) {



let args = [...arguments]

arr.filter(x=>x===args[2]||args[3]);

console.log(arr)

  return arr;

}

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

What does this mean?

and this?

Each side of the logical OR is treated as a complete expression.

1 Like

In English you can be a little more loose with your phrasing. For example, when you mean to say “if A is equal to B, or A is equal to C”, you can reduce that to “if A is equal to B or C”.

JavaScript is not English however. What you’ve written is being interpreted as “if A is equal to B, or C exists”.

I decided to try a for loop to take a break from trying to crack how filter() would solve this (I know it can but I can’t quite figure it out right now).

The for loop is not working and I’m stuck again. The logic seems clear to me, but it’s returning things that are the complete opposite of what I anticipated and I’m not sure why. Help please.

Thank you,
Nick

function destroyer(arr) {

let args = [...arguments].slice(1);

let newArr = [];

console.log(args)

for(let i = 0; i < arr.length; i++){

for(let j = 0; j < args.length; j++){

    if(arr[i]!==args[j]){

     newArr.push(arr[i]);

    }

}

}console.log(newArr)

  return newArr;

}

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

This says “every single time an argument that does not match the value of arr[i], push arr[i] onto my result array”. That can’t be what you meant to do.

I solved the problem, but I have some questions below (underneath the code) about the mechanisms at work.

  let args = [...arguments];
  console.log(args)
  return arr.filter(x=>!args.includes(x));

filter() would strain out all elements that are not-args.
Yet, when I console.log(args), the elements that I want to retain are included in args. along with those I want to get rid of.
I am not specifying an index of args ( as I thought I should do in previous attempts ); yet, it seems to naturally filter out the two extra arguments at the end of args.

What’s going on here?

The first argument is an array. The elements of that array will never be the array itself.

So, for the first argument, filter() sees an array-object argument and does not recognize the elements within this array. The next two are individual element arguments. Correct?