Seek and Destroy unable to filter the array

Seek and Destroy unable to filter the array
0

#1

Tell us what’s happening:

used spread operator access the arguments.
When I filter the newArr nothing happens and I am returned with same array.

Can somebody please help me , why the filter is not working ?

Your code so far


function destroyer(...arr) {
  // Remove all the values
  //used spread operator to access the arguments
  let dest = arr.slice(1);
  let newArr = arr.slice(0,1);
  //using filter to remove elements in newArr which are present in dest
  return newArr.filter((item) => !dest.includes(item));
}

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/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/seek-and-destroy


#2

I think you are not aware what your ...operator is doing when used on arr.

Inside the function arr is [ [ 1, 2, 3, 1, 2, 3 ] ], so dest = [ 2, 3 ] and before the filter method is called on newArr, newArr looks like [ [ 1, 2, 3, 1, 2, 3 ] ].

Is that what you thought those variables would contain before the filter?

Just specifiy arr as the first parameter which will capture the first argument passed in (the array) and then use ... to create a new variable which will be an array containing the rest of the arguments passed into the function.

This way, you can apply the filter on arr and return only arr elements which are not included in the new variable variable array.


#3

@randelldawson thanks for replying.

I did log out the arr but overlooked the nested array.

and completed the challenge. :slight_smile: