Hello guys, so I’m making this algorithm challenges and the Seek and Destroy got me quite confused.
So I was trying to access the function’s aruments through argument[x] where I would iterate through them in a for loop.
It didn’t work.
It started working when I transformed the arguments into an array. But when I tried to log arguments[x] (let x be any number) into console it worked.
So here is the correct code:
function destroyer(arr) {
// Remove all the values
var args = Array.prototype.slice.call(arguments);
var newArr = arguments[0].filter(function(val){
for(var x = 1; x < args.length; x++){
if(val == args[x]){
return false;
}
}
return true;
});
return newArr;
And this is the incorrect code:
function destroyer(arr) {
// Remove all the values
var newArr = arguments[0].filter(function(val){
for(var x = 1; x < arguments.length; x++){
if(val == arguments[x]){
return false;
}
}
return true;
});
return newArr;
}
My question is how come one works and the other doesn’t?