I've done the following freeCodeCamp exercise. It involves eliminating elements of a array
according to numbers received in parameter.
In the call to the function destroyer
a array
appears and, as second and third parameters, the numbers that must be deleted from said array.
This is the code:
function destroyer(arr) {
var args = Array.prototype.slice.call(arguments);
args.splice(0, 1);
return arr.filter(function(element) {
return args.indexOf(element) === -1;
});
}
console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));
The first instruction is clear: convert the arguments of the function into a real array in order to work with it.
The second instruction separates the first argument to keep the numbers to search. But when I debug the process, I check that arr, the parameter received by the destroy function, only contains [1,2,3,1,2,3]
Why does this happen? Should not it contain the complete parameter that was sent when invoking the function? that is: ([1, 2, 3, 1, 2, 3], 2, 3)
.
That's why I do not understand the third part, when the filter is applied on the arr
parameter. At what point did the arguments 2,3 of destroyer
disappear from the parameter received by arr
?
And one last question. Why are there two returns? Is one the response of the callback and the other is the answer of destroyer?