Seek and Destroy- Don't know how to pass third argument

Hello everyone, I’ve been quite stuck on this algorithm. I think I am close enough to the solution, but I don’t know how to pass the third argument through the while loop.

The code returns: [ ‘hamburger’, 53 ] and it should return just [ ‘hamburger’] instead. I think there’s something wrong with my while loop, but I don’t know how to fix it. Any suggestion would be helpful, thank you :slight_smile:

My code:


function destroyer(arr) {
  // Remove all the values
  var args = Array.prototype.slice.call(arguments);
  var newArr = arr.filter(function(val) {
    var i = 1;
    while (i < args.length){
      //console.log(args[i]);
      if (val === args[1]) {
        i++;
        return false;
      }
      else {
        i++;
        return true;
      }
    }
  });
    
  return newArr;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36.

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

The first issue is that you hard coded the index to 1 in the following if statement.

      if (val === args[1]) {

You are only checking if val is equal to the second element of args every iteration of the filter method.

1 Like

Your code is very complicated.

Split arr in 2 parts. First element and rest of the array.
Then check every element of the first against the rest using Array.filter and Array.includes methods and return the result.

1 Like