i just completed this bonfire, but i am not sure why the final solution was right. also is my callback redundant since .filter() iterates through the array anyway?
function destroyer(arr) {
var init = arguments[0];
var full = Array.from(arguments);
var seek = full.slice(1, full.length);
var slicer = function(){
for(var i = 0; i < seek.length; i++){
for(var k = 0; k < init.length; k++){
if(seek[i] === init[k]) {
init.splice(k, k+1);
console.log("chopping " + init);
}
}
}
};
init.filter(slicer);
console.log("final " + init);
return init;
}
destroyer([2, 3, 2, 3], 2, 3);
MDN says the second argument in splice is the amount of elements to remove. i originally intended to remove 1 at a time, but ran into trouble where this version was not returning an empty array. i tried splicing from the end, from the beginning, but ultimately this (k+1) is what worked for me. can anyone explain my why removing more than 1 element did the trick?
on a side note, as far as DRY is concerned, is running a nested loop inside .filter() super wet since filter loops through the array, and then i am running an additional loop?
much appreciated.