freeCodeCamp Challenge Guide: Seek and Destroy

var args = [...arguments].slice(1)

How did I not think of that… I stared at the problem for 10 minutes thinking what to do with the arguments object and the best I could some up with was an empty array and a for loop to push all the args into that array.

1 Like

Here is my solution. It looks quite different from those in the wiki. Could someone take a quick look and let me know if it’s acceptable?

function destroyer(arr) {
  
  // Create two new arrays, one for the initial array and another for the characters to remove
  var initialArr = arguments[0];
  var toRemove = [];
  
  // Push all other args to 'toRemove' array
  for (i=1; i<arguments.length; i++) {
    toRemove.push(arguments[i]);
  }
  
  // Filter out unwanted chars from the initial array
  for (i=0; i<toRemove.length; i++) {
    initialArr = initialArr.filter(function(num) {
      return num != toRemove[i];
    });
  }
  
  return initialArr;
  
}

Another solution of seek and Destroy
function destroyer(arr) {
// Remove all the values
for(var i =1 ; i<arguments.length; i++){
var min=i;
var max=arguments[min];
arr=arr.filter(function(value){
if(value===max){
value-=max;
return value;
}
else{
return value;
}
});
}
return arr;
}

this seems good to me. if i am reading it correctly:

  • you make 2 arrays
  • then your last loop compares them an index at a time
  • if there is no match that index basically get rewritten into initialArr
  • if there is a match that index won’t get rewritten into initialArr

i figure as long as it works it good

  • yours actually similar to mine in logic, we just go about it different in procedures

the freecodecamp coding system throws a warning when i use the spread operator but it lets me use it in the code so i figure why not take advantage of it

Very basic solution but still working fine. Working with indexOf method.

function destroyer(arr) {
  var args = [];
  
  for (var i = 1; i < arguments.length; i++) {
    args.push(arguments[i]);
  }
  
  var filtered = arr.filter(function(x) {
    return args.indexOf(x) === -1;
  });
  
  
  return filtered;
}

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

I’ve been stuck on this exercise for days and I’m really struggling with the filter and callback methods.

So regarding the Basic Code Solution:

[code]function destroyer(arr) {
var args = Array.prototype.slice.call(arguments);

for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < args.length; j++) {
if (arr[i] === args[j]) {
delete arr[i];
}
}
}
return arr.filter(Boolean);
}[/code]

I see what you’re doing with the two for-loops and I’ve attempted several solutions with a similar structure. My problem was always accessing the original arguments and going from there.

Could someone please explain to me what’s going on here:

Why not go for 2 arrays (one with the numbers we want to test against and one with the numbers to test with)? Why not use arguments[0]? What exactly does this line of code produce? What does “var args” actually look like? Is it:

?

I apologize if these seem like dumb questions, but after banging my head into a wall for a while I figured I needed to go back to the absolute basics and make sure I actually understand everything.

This is one of the solutions I attempted that does not work. I’m including it here because I think it illustrates what I struggle with:

function destroyer(arr) {
  var array = Array.prototype.slice.call(arguments[0]);
  var test = Array.prototype.slice.call(arguments[1]);
  var newArray = [];
  
  for (var i = 0; i < array.length; i++) { 
    for (var j = 0; j < test.length; j++) { 
      if (array[i] !== test[i]) { 
        newArray.push(arr[i]);
      }
    }
  }
  return newArray;
}

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

Hey guys, any idea why isn’t this working? I’ve been trying to debug it in the console but can’t seem to get it going. I’d appreciate your help.

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

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

function destroyer(arr) {

// Create arguments array
var myArguments = [];
for (var i = 1; i < arguments.length; i++) {
myArguments.push(arguments[i]);
}

return arr.filter( function(remove) {
// if -1 will be true
return myArguments.indexOf(remove) < 0;
});

}

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

/*
Unfortunately, I didn’t do this lesson alone because I didn’t know de argument object. But I found this solution on Google:


Thanks a lot, I understood better after to read this article
*/

1 Like

function destroyer(arr) {
// Remove all the values
var args = Array.from(arguments);

for (var i = 1; i < args.length; i++){
arr = arr.filter(function(ele) {
return ele !== args[i];
});

}
return arr;
}

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

I passed the challenge with this code but I get the message ''functions declared within loops referencing an outer scoped variable may lead to confusing semantics". How do I rewrite my code to avoid this message? Don’t really understand what it is telling me.

So I finally got it working this way:

function destroyer(arr) {
  var toRemove = [];
  var target = arguments[0];
  for (i = 1; i < arguments.length; i++) {
    toRemove.push(arguments[i]);
  }
  
  var results = target.filter(function(val){
    for (i = 0; i < toRemove.length; i++) {
      return toRemove.indexOf(val) < 0;
    }
  });
  // Remove all the values
  return results;
}

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

Shows me I’ve still got so much to learn.

1 Like

I am happy to have finished this task once again.

function destroyer(arr) {
  let args = Array.from(arguments).slice(1);
  return arr.filter(item => !args.includes(item));
}

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

This isn’t the most elegant solution, but I decided to use the tools I had already learned. I was stuck on this for a couple of hours yesterday getting lost in syntax. Today, on my morning walk, I though the problem through then came home and pseudocoded it out on paper. I came up with something different from the provided answers, but much simpler for me to wrap my head around. This solution works for any amount of arguments. Please let me know what I could improve.

function destroyer(arr) { arr = Array.from(arguments); //-------- turn arr into an array containing all elements. var arr1 = arr.shift(); //------------- cut the total array into arr1, leaving arr as the arguments. var currentNum = []; //---------------- creates an array to store current argument to test. while (arr.length > 0) { //------------ iterates through until arguments stored in arr are all gone. currentNum = arr.splice(0, 1); //------ cuts current argument to test, leaving arr[0] as the next argument. arr1 = arr1.filter(function(val) { //-- sets arr1 to filtered arr1. return val != currentNum; //----------- arr1 becomes everything that doesn't match currentNum. }); //--------------------------------- loops back to filter next argument. } return arr1; //------------------------ when all arguments have been filtered, returns filtered array. }

destroyer([1, 2, 3, 1, 2, 3, 4, 5, 6], 2, 3, 4, 5, 6); // returns [1,1]

Took me a rather long time to figure this out, my code looks like spaghetti code

function destroyer(arr) {
  
  
  //Store unspecified array size to argArr using arguments. 
  //newArr is result
  var argArr = Array.prototype.slice.call(arguments);
  var newArr =[];
  
  //Initialize var for filter function checks
  var argLength = arguments.length;
  var test = false;
  
  //Filter function, sends each val from array, loops however many arguments needed
  function seekDestroy(val){
    for (let i =1; i<argArr.length; i++){
      if (val==argArr[i]){
        test = false;
        break;
      } else{
        test = true;
      }
    }
    return test;
    }
  
  newArr = argArr[0].filter(seekDestroy);
  return newArr;

}

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

brief explanation:

  1. Store arguments in argArr
  2. Make newArr to store results
  3. Filter function loops check array element in arr[0] with all values from nonarray elements (arr[1], arr[2], arr[3]…etc) arguments
  4. Store conditional check and break loop if something is found
  5. Return that value

EDIT doing solution again a few days later

function destroyer(arr) {
  // Remove all the values
  var arg = Array.prototype.slice.call(arguments);
 
   return arr.filter(seekDestroy);
  
  function seekDestroy(val) {
    return !arg.includes(val);
  }
  
  return arr;
}

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

similar to intermediate solution but not as efficient in that the includes uses the whole arg array rather than slicing off the first part of array

This is how I did it. If there were more than three elements I would have run into trouble though!

function destroyer(arr) {

//convert into a new array with all arguments
var args = Array.from(arguments);

//if the first element in the new array is an array object:
if (typeof args[0] === ‘object’) {
//splice to a new array containing that first element
var splicedArr = args.splice(0,1);
}

//argValue is the current element of the splicedArr array
function checkVals (argValue) {
if (argValue === args[0]) {
return false;
}
if (argValue === args[1]) {
return false;
}
if (argValue === args[2]) {
return false;
}
else {
return true;
}
}
//if checkVals returns true the element of splicedArr will be kept
var filteredArr = splicedArr[0].filter(checkVals);

return filteredArr;
}

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

My ugly code.

function destroyer(arr) {
  // Remove all the values
  
  var args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments));
  for (var i=1;i<arguments.length;i++){
   arr= arr.filter(function (x){
     return x!=args[i];
  });
  }
  
  
  return arr;

}

Here is another solution:

 function destroyer(arr) {
      // Remove all the values
      var args = Array.from(arguments).slice(1);
      return arr.filter(function(el){
        return args.indexOf(el) == -1;
      });
    }

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

Another one:

var arg=0;
function different(value){
return value !== arg;
}
function destroyer(arr) {
// Remove all the values
var destroy =arr;
for (i=1;i<arguments.length;i++){
arg = arguments[i];
destroy = destroy.filter(different);
}
return destroy;
}

destroyer([“tree”, “hamburger”, 53], “tree”, 53);

This is my solution:

function destroyer(arr) {
var args = [].slice.call(arguments);
return arr.filter( function (a) {
return args.indexOf(a) === -1;
});
}

1 Like

My 2 cents (very similar to solutions above):

function destroyer(arr) {
var args = Array.from(arguments);          //create an array in args with all function arguments
  for(i = 0; i < args[0].length; i++){    //loop through the first array in args
    for (j = 1; j < args.length; j++){    //then loop through second array in args
      if(args[0][i] === args[j]){         //if value in args[0] = any value of array in args[j], delete.
        delete args[0][i];
      }
    }
  }
  
  return args[0].filter(Boolean);   //filter values, then return values when true
}

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

P.S: I had to look up solutions for this one. It seems to be quite hard for beginners. Credit to Christine Javier in her video https://www.youtube.com/watch?v=Gz9ECvouuR0 .