Seek and Destroy not removing duplicate elements

Tell us what’s happening:

Your code so far
function destroyer(arr) {
// Remove all the values
if(arguments.length > 1){

for(let i=1;i<arguments.length;i++){
  let x = -1;
  while(x=arr.indexOf(arguments[i],x+1) != -1){
    arr.splice(arr.indexOf(arguments[i]),1);
  }
}

}

return arr;

}

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


function destroyer(arr) {
  // Remove all the values
  if(arguments.length > 1){
    
    for(let i=1;i<arguments.length;i++){
      let x = -1;
      while(x=arr.indexOf(arguments[i],x+1) != -1){
        arr.splice(arr.indexOf(arguments[i]),1);
      }
    }
  }

  return arr;
  
}

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

Your browser information:

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

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

dont use arguments. it will just confuse you. just use what every you already have as arr

arr[0] is what you want to scan and everything else are the targets

how can take turns comparing arr[0] to each of the other items in arr

Not sure this is the best solution, but i have figured out this approach.

for(let j=1;j<arguments.length;j++){
    let x = arguments[j];
     arr.forEach(function(element,pos){
        if(element===x){
           arr.splice(pos,1);
         }
     });
     //since forEach iterator doesn't move till last element.
     //in case last element is matching with argument.
   if(arr[arr.length-1]===x){
     arr.splice(arr.length-1,1);
   }     
}

Gotta love functional programming:

function destroyer(arr, ...args) {
  return arr.filter(n => !args.includes(n));
}
1 Like