Seek and Destroy from Intermediate Algorithm Scripting

I was doing this exercise and i pass it but not intentionaly and i don’t really understand how this code works if someone can explain me i will be very glad.

This is the exercise: You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.

Note: You have to use the arguments object.

This is my code:

function destroyer(...args) {
  let newArr = args.concat().flat()
 .filter(el => !args.includes(el));
  // console.log(args.concat().flat())
  return newArr;

destroyer(["tree", "hamburger", 53], "tree", 53)

I don’t get this part of the code:
el => !args.includes(el)); since the filter is applaid on i guess this array [ 'tree', 'hamburger', 53, 'tree', 53 ] because i make all the arguments in one only array.

This is a solution from FCC Hints and can see how it works:

function destroyer(arr, ...valsToRemove) {
  return arr.filter(elem => !valsToRemove.includes(elem));

because here the array is separated from the rest of the arguments.

Sry if im don’t explaint it well, Inglish is not my native language , hope its understable what i wrote

So, you’ve copied an answer, I assume. And there is another answer you’re asking about.

But afaics, both of these are answers to the challenge from before this condition was added:

Note: You have to use the arguments object.

As neither use the arguments object, although we can help you understand how they work, they aren’t really valid answers to the challenge.

The code i wrote it myself but i don’t expected it to pass the test but it did (i allways put console.logs() while typing code so i get a hint of what would return).

I know that it doesn’t use the arguments object but the test passes so i tried to understand why, and one of the solutions that FCC offers in the Challengue guide is the one that i put in, i saw it after already pass it with my code.

So yea, if you can explain me how my code works, expecially the part that i highlight in the original question would be glad.

If you print what your args look like, it will show this:

[ [ 'tree', 'hamburger', 53 ], 'tree', 53 ]

In your filter code, you say "keep the element if it is not included in args. ‘tree’ and 53 are part of args, so you are gonna get rid of an element equal to one of those.

Ohh nice, since i console.log this: console.log(args.concat().flat()) and that show me a flat array like this one [ 'tree', 'hamburger', 53, 'tree', 53 ] i thinked the filter applay on this flat array and that confused me but it seems that applays only in the args array, so thank you!

the filter applies on [ 'tree', 'hamburger', 53, 'tree', 53 ], but the includes() applies on [ [ 'tree', 'hamburger', 53 ], 'tree', 53 ].
With arr.filter(callback), you loop throught every arr element and test it in the callback function. If the callback returns true for the given element, the element is left in the resulting array, otherwise its filtered our. In your case, each element from the flatten out array is tested if is included in the arguments array, and only kept in the resulting array, if it is not included.

Here i formatted your solution, using more descriptive variable names and including some logs to follow the code behavior:

function destroyer(...args) {
  const availableElements = args.concat().flat()
  console.log('All elements:', availableElements)
  console.log('Arguments:', args)
  const filteredArray = availableElements.filter(element => {
    const isElementIncluded= !args.includes(element)
    console.log(`Is ${element} included in the arguments?`, isElementIncluded)
    return isElementIncluded
  console.log('Result:', filteredArray)
  return filteredArray;

destroyer(["tree", "hamburger", 53], "tree", 53)
1 Like

Great! Thanks for your time, now its crystal clear.

1 Like

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