This code is not working:
function destroyer(arr) {
// Remove all the values
var result = arr;
for (var i = 1; i < arguments.length; i++) {
result = result.filter(function(val) {
return val != arguments[i];
});
}
return result;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
But when I assign the value of arguments[i]
to another variable:
var arg = arguments[i];``` And use
arg` instead, it works:
function destroyer(arr) {
// Remove all the values
var result = arr;
for (var i = 1; i < arguments.length; i++) {
var arg = arguments[i];
result = result.filter(function(val) {
return val != arg;
});
}
return result;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
Can anyone explain such a behaviour?
Also, I get this warning:
Don’t make functions within a loop.
Which probably means I shouldn’t make functions within a loop
Can someone explain this also.