I do online JavaScript courses and am curious about one of the tasks:
We are equipped with an initial array (the first argument in the destroyer function), followed by one or more arguments. We must remove all elements from the original array that have the same meaning as these arguments.
Here is my solution, but it does not work:
function destroyer(arr) {
var filterArr = [];
for (var i = 1; i < arguments.length; i++) {
filterArr.push(arguments[i]);
}
console.log(filterArr);
function filterIt(value) {
for (var j = 0; j < filterArr.length; j++) {
if (value === filterArr[j]) {
return false;
}
}
}
return arguments[0].filter(filterIt);
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
Run codeHide resultI was able to find a solution, but for me it makes no sense, so I am posting this question; could you tell me why the following code works:
function destroyer(arr) {
var filterArr = [];
for (var i = 1; i < arguments.length; i++) {
filterArr.push(arguments[i]);
}
console.log(filterArr);
function filterIt(value) {
for (var j = 0; j < filterArr.length; j++) {
if (value === filterArr[j]) {
return false;
}
}
return true;
}
return arguments[0].filter(filterIt);
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
Run codeHide resultThanks for the heads!
source
share