Seek and Destroy(Confusion on argument object)

Seek and Destroy(Confusion on argument object)
0.0 0

#1

Tell us what’s happening:
Why is the result different of these two cases:
function destroyer(arr) {

var a = arguments;
var newArr = [];

for (var i = 1; i < arguments.length; i++){
newArr = arr.filter(function(val){
return val != arguments[i];
});
return newArr;
}
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);

VS

function destroyer(arr) {
// Remove all the values
var a = arguments;
// var length = a.length;
var newArr = [];

for (var i = 1; i < arguments.length; i++){
newArr = arr.filter(function(val){
return val != a[i];
});
return newArr;
}

}

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

Your code so far

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

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

Your browser information:

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

Link to the challenge:


#2

I would suggest reading up on both arguments and filter.

Just saving arguments into a variable does not do what you might think.
If arguments is [1, 2, 3, 1, 2, 3], 2, 3. You need to separate the 2 and 3 out to use later.


.filter() loops through an array on its own so the addition of a for loop might not do what you want. Your for loop as is runs once, hits the return statement at end of first iteration and exits.

.filter() tests each element in array with a callback function you provide. Elements that pass the test get put in a new array that is the return value.

var filteredArray = unfilteredArray.filter(someTestFunction);

Good luck!