*Intermediate Algorithm Scripting: Seek and Destroy

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

I have already done a method to filter the elments which are differents from the array1 to array2, but I do not how to separate functionally the argument in the function destroyer. Therefore, I want to make the arra1 with the elements inside the array and another array (arr2) with the rest of the elements.

function destroyer(arr) {
  
  
   arr1.filter(element => !arr2.includes(element))
  
  return arr;


}

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

arr1 and arr2 are not defined in your code. Also, filter returns a new array, so you need to do something with the returned value of it

1 Like

I have already definated arr1, but I need to define the another one. How may I do it? This has to have 2 and 3 in this case.

function destroyer(arr) {

 let  arr1=arr.slice();
 
 let arr2

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


                     
   }

    
   //arr1.filter(element => !arr2.includes(element))
  
  return arr;


}

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

have you already met the rest operator? this seems exactly he case in which it can be used.

1 Like

sorry, this one wasn’t actually a duplicated topics, my bad. re-listed it again

1 Like

Thanks for making that. I was not understanding you.
I want to ask something aboout the code. I am getting the same result with that operator.

function destroyer(arr) {

 let  arr1=[...arr].slice();
 
 let arr2=[...arr].slice()

 console.log()
   

    
   //arr1.filter(element => !arr2.includes(element))
  
  return arr;


}

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

It is done. Thanks!

function destroyer(...arr) {

 let  arr1=arr[0];
 
 let arr2=arr

 //console.log(arr2)
   

    
   let newArr=arr1.filter(element => !arr2.includes(element))
  
  console.log(newArr)
  return newArr;


}

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

you can add parameters to the function definition.
So you can have the array passed in, and an other array that with the rest parameter will hold the numbers to remove

1 Like