Seek and Destroy need help with my logic

Seek and Destroy need help with my logic
0.0 0

#1

Tell us what’s happening:

Can somebody help how to solve this task. I went over it several times and I cannot find my logical mistake. A hint would be great. Thanks :slight_smile:

Your code so far

function destroyer(arr) {
  // Remove all the values
  var result = arr.filter(newArr);
  
  function newArr(val) {
  for (var i = 1; i < arguments.length; i++) {
    for (var j = 0; j < arr.length; j++) {
        if(arguments[i] !== arr[j]) {
          return val;
        }
      }
    }
  }
    
  
  return result;
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0.

Link to the challenge:


#2

Look at what’s inside arguments:

function destroyer(arr) {
  console.log(arguments)
  // Remove all the values
  var result = arr.filter(newArr);
  
  function newArr(val) {
    console.log(arguments)
    for (var i = 1; i < arguments.length; i++) {
      for (var j = 0; j < arr.length; j++) {
        if(arguments[i] !== arr[j]) {
          return val;
        }
      }
    }
  }
    
  
  return result;
}

console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));

#3

Arguments are what’s passed into the function. You’re working with two functions. Each function has its own arguments object.


#4

The following is mine, there are simmilar way of thinking but different way of code

function destroyer(arr){
    //convert arguments into array
    
    var args = Array.from(arguments);
    
    //comparing the indexes of array and arguments
    //and delete if it's equal
    for(var i in arr){
        for(var j in args){
            if(arr[i] === args[j]){
                delete arr[i];
            }
        }
    }
    // the expected result of arr would contain some null values 
    //therefore, we need to filter() those null 
    return arr.filter(arr => Boolean(arr));
}


#5

Instead of just posting your solution (which the OP may not want to see just yet), try giving him hints what is currently causing his current solution to not pass the challenge.

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.


#6

thanks for your reminding :slight_smile: