Seek and Destroy //code not working

Seek and Destroy //code not working
0

#1

Tell us what’s happening:

Your code so far

function destroyer(arr) {
  // Remove all the values
  
    return arr.filter(function(word){
     for(var i=1; i<arguments.length;i++){ 
       if(word===arguments[i]){
          return false;   
     }
    }
      return true;
  });
 
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0.

Link to the challenge:


Doesn’t arr.filter() include the values which are ‘truthy’? So, after checking if the word is equal to the arguments[i], and then setting return to false or true accordingly, shouldn’t the function return an array with the truthy values?


#2

The code always returns true. What you might want to do is:

let returnValue = true

  for(let i = 1; i<arguments.length; i++) {
		if(word === arguments[i]) { returnValue = false }
  }

return returnValue

#3

I added a few console.log statements so you can see what is going on in your code. You probably were not understanding how the filter was working.

function destroyer(arr) {
  // Remove all the values
  
  return arr.filter(function(word){
    for(var i=1; i<arguments.length;i++){
      console.log('word = ' + word);
      console.log('arguments[' + i + '] = ' + arguments[i]);
      console.log();
      if(word===arguments[i]){
         return false;   
      }
    }
    return true;
  });
}

Running the above code results in:

word = 1
arguments[1] = 0

word = 1
arguments[2] = 1,2,3,1,2,3

word = 2
arguments[1] = 1

word = 2
arguments[2] = 1,2,3,1,2,3

word = 3
arguments[1] = 2

word = 3
arguments[2] = 1,2,3,1,2,3

word = 1
arguments[1] = 3

word = 1
arguments[2] = 1,2,3,1,2,3

word = 2
arguments[1] = 4

word = 2
arguments[2] = 1,2,3,1,2,3

word = 3
arguments[1] = 5

word = 3
arguments[2] = 1,2,3,1,2,3

If you still don’t understand what is going on, let me know.