Seek and destroy help with .filter

Could someone help me with this please, I dont understand why the filter line of code is not working, and why it is creating two arrays instead of one?

  **Your code so far**
 function destroyer(arr) {
//console.log('args:', arguments)

let args =[] //2,3
console.log('arr:', arr)
for (let i=1; i <arguments.length; i++){
args.push(arguments[i])
console.log('args:', args)
}

for (let j=0; j <args.length; j++){
   
   let filtered = arr.filter(num => num !== args[j])
   
console.log(filtered)
}

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/93.0.4577.63 Safari/537.36 Edg/93.0.961.38

Challenge: Seek and Destroy

Link to the challenge:

I have also tried this: but it says num is not a function

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

     if (arr.includes(args[j])){

     let filtered = arr.filter(num => num.indexOf(args[j]) >= 0)

  }

  }

Consider combining those, and when you do, you won’t need the for loop any longer.

newArr = newArr.filter(num =>
  /* how could you say args does 
   * not include num here? 
   */)

Simply doing that checks each member of newArr against each member of args without an explicit loop.

Hi, sorry I have edited the code since so it is a bit different. Regardless of that I still dont understand why I would be checking if args contained num isnt it the other way round?

The final return is the array, without certain elements. What determines if a given element is to be removed from the array?

Define the original problem, in your own words. Not code…words.

So I am trying to remove the elements that are the same as each argument in the args array, so if the element in arr is not equal to an element in args I want to maybe push that element in arr into a new array and return this?

Sounds like a filter to me: if the args includes a value that you find in newArr, that value shouldn’t be kept in newArr… Which goes back to that filter function. How can you, in code, say “not if arr includes this num?”

1 Like

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