Hey friend let me try to explain.
Destroyer waits only for one argument to be passed. Since we pass more then one argument it will assign only first one to “arr” and it will be array of numbers. But we still have the “arguments” object that has ALL the arguments passed, so we create an array “args” that will contain them.
-
“args” - this variable will hold the array of arguments that were passed to the “destroyer” starting from 2nd argument (because we spliced the first argument which was an array of numbers);
-
We use built-in Array method “filter” against the “arr” variable that is our array of numbers (first argument). Callback function in filter, will be passed with current iteration’s number in the array, for e.g.:
[1,2,3,4].filter(function(element) { return element === 3; }) - it will start from 1, but 1 != 3, go to 2 but !=3, then encounter 3 that === 3 and add it to the new filtered array and will go on until all numbers in the [1,2,3,4] array are covered.
So ho does it filter? You see [1,2,3,4].indexOf(2) returns the index of the first 2 that it encounters. The index of that 2 will be 1 (because start from 0). But if if .indexOf(item) won’t find this item in the array it return -1. Finally:
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
arr = [1, 2, 3, 1, 2, 3];
args = [2, 3];
apply filter to "arr"
1 iteration - "do we have 1 in array “args”? returns -1 because we dont!
2 iteration - "do we have 2 in array “args”? returns index of 2 in args and it’s 1 (every number that is >0 is considered as "true) so it makes it’s way to the new filtered array;
3 iteration - "do we have 3 in array “args”? returns index of 3 in args and it’s 2 (every number that is >0 is considered as "true) so it makes it’s way to the new filtered array;
4 iteration - "do we have 1 in array “args”? returns -1 because we dont! so this 1 doesn’t make it’s way to the new filtered array.
Hope that explains the logic behind all that. For “Truthy, Falsy values” please read this paragraph - http://speakingjs.com/es5/ch10.html#truthy_falsy