Query about length property in Seek and Destroy

Tell us what’s happening:

Two questions:
I’m confused as to why the length of the arr array has a length property of six. I count only five in the arr array. What am I missing? I’m assuming that the numbers outside of it each have their own individual length since they are not connected to an array. Am I incorrect in making this assumption?

Your code so far


function destroyer(arr) {
 var len = arr.length;
 console.log(len)
 var lenf = destroyer.length;
 console.log(lenf+len)
  var args = [...arguments];

  args.filter((val) =>{
      if(arr.includes(!arr)){
        
      }
      })
  return ;
}

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

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

This is arr, and those are six numbers… the length is six

I see a few little errors in your code, for example arr.includes(!arr) will not due what you want. ! invert a Bolean, an array is truthy and you are practically checking arr.includes(false)

At the moment, I’m fairly certain that I just need filter() and an if statement to remove all elements of the same value as those outside the array arr. Yet, the above statement doesn’t seem to hold true. It makes sense to me that it should so would someone mind explaining why it’s not working?
Also, I would like to know if I should pursue an if loop with what I already have or if that is overkill?(I have at commented out because I’m not sure whether it’s necessary or not.)

Thank you!


function destroyer(arr) {
  var args = [...arguments] //collect all element under args
  var subArgs = [];         //create containers for
  var finArgs = [];         //further collecting
  args.filter((value)=>{    //filter args pushing elements to be removed
    if(value in arr){       //into subArgs container
      subArgs.push(value);
    }
  });
/*
for(var i = 0; i < arr.length; i++){  
if(arr[i] < subArgs){
finArgs.push(arr[i]);
}

} 

return finArgs
*/
return args;
}

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

You are actually quite close.

You are not quite grasping what is in args though. Your args array contains [[1, 2, 3, 1, 2, 3], 2, 3] because arguments is ALL arguments.

So your first step will be to segregate [1, 2, 3, 1, 2, 3] (which you actually already have in arr) and [2,3]. You could use array.shift() to remove that first element or you could do something with destructuring and the rest parameter. Either way you need variable holding [1, 2, 3, 1, 2, 3] and another holding [2,3]

Once you have the above covered you can filter out all the 2’s and 3’s from arr

I am not passing any test, so I assume that I am returning my final container variable to soon
or
that I am not passing my variable through filter() in the proper way, that it doesn’t have enough interaction with for loop to receive the essential benefits of it.
Where am I going wrong?


function destroyer(arr) {
  //collect all elements in single variable
  var args = [...arguments];
  //create final container for result
  var finArr = [];
  //shift() args to separate 
  args.shift()
  console.log(args)
  console.log(arr)
  //filter val through for loop
  arr.filter((val)=>{
   for(var i = 0; i < arr.length; i++){
  //condition of filter is value in arr that does not equal value in args should be assigned to val and pushed into finArr
   if(arr[i] !== args[i]){
     arr[i] = val;
     finArr.push(val);
     console.log(finArr)
   }
     
   }
})
return finArr;

}

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

Recheck how filter() works, its callback must return true or false. It is not returning anything. If you want to do it that way you may prefer using forEach() instead.

Your if statement inside the filter method has some issues too… you are checking arr[i] against args[i]. What happens when i is 4? The things inside the if statement are never executed.
Using filter is a good intuition, but you are applying it in the wrong way.
For example:
let final = arr.filter((val) => {return val > 10;}
In this case, after the filter method was applied, now final has inside only the items in arr for which the callback function in the filter method returned true. (In this case only numbers bigger than 10)


You are making this way harder than it has to be. read about the rest operator, the filter method, and the includes method. This can be solved in one line. here’s a hint: don’t be afraid to modify the parameters of destroyer() (you did read about the rest operator, right?)

I read that stuff and I can understand how it should all fit. The spread operator separating the parameters of the function into two different ones and the filter run on arr so it creates a new array that does not include any of the matching elements between the two elements in arr. And it does fit on one line which feels strangely pleasant to see.

Yet, it’s not working and I don’t know where I’m wrong. I’m filtering out all the elements from args and returning a new array sans those.

???


function destroyer(arr,...args) {
  var newArr = arr.filter((val)=>{!arr.includes(args)});
  console.log(newArr)
  return newArr;
}

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

var newArr = arr.filter((val)=>{!arr.includes(args)});

with arrow functions, you either use brackets and return or, if you only have one line, no brackets and an implicit return. so you can either do:

var newArr = arr.filter((val)=>{
    return !args.includes(val); // also, you are testing to see if `val` is included in `args`
});

or

var newArr = arr.filter((val) => !args.includes(val));

Solved it. Thank you.