Intermediate Algorithm Scripting - Seek and Destroy

Can someone tell me why this code does not work

function destroyer(arr) {
  let arr2= Array.from(arguments).slice(1);
  let arr3=[];
  for (let i=0; i < arr; i++)
  for (let j=0; j < arr2; j++) {
  if (arr[i]!==arr2[j]) {
arr3.push (arr[i])
  } 
  }
 return arr3
};

console.log(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/113.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Seek and Destroy

Link to the challenge:

Indenting your code:

function destroyer(arr) {
  let arr2= Array.from(arguments).slice(1);
  let arr3=[];
  for (let i=0; i < arr; i++) {
    for (let j=0; j < arr2; j++) {
      if (arr[i] !== arr2[j]) {
        arr3.push(arr[i]);
      } 
    }
  }
  return arr3;
}

I would double check your logic. Every single time arr[i] is not equal to one of the elements of arr2 you add it to the arr3? Have you seen how much stuff you’re putting into arr3?

The console just says ‘’ every time. Is it okay if you just spell it out what I am doing wrong because I am still confused to be honest

Also need to fix this - arr2 isn’t a number

I’m trying to do that. I’m still cannot give you the solution code.

Can you give me any more information?

What have you done about this? What sort of thing is arr2?

yes I have changed this to arr2.length

and here too

Yep have done that one now

Ok. So that brings us around to

What’s coming into arr3 now?

1,1,2,3,1,1,2,3. It has doubled the 1s, joined arr 1 and arr2 together, and taken a two out… Not sure why

This says to add arr[i] to the array arr3 every single time it doesn’t match one of the elements in arr2. I think you want instead to add arr[i] to arr3 only if it doesn’t match every single element in arr2.

Given what you said, I changed it to this and it worked (apologies if formatting is bad)-

function destroyer(arr) {
  let arr2= Array.from(arguments).slice(1);
  let arr3=[];
  for (let i=0; i < arr.length; i++) {
    for (let j=0; j < arr2.length; j++) {
      if (arr[i] === arr2[j]) {
        arr3.push(arr[i]);
      } 
    }
  }
return arr.filter(val => !arr3.includes (val))
};

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.