I am currently in the process of re-learning JavaScript, and unfortunately, I'm facing some difficulties.
Here's the challenge at hand:
In the destroyer function, you will receive an initial array as the first argument. Following that, one or more arguments will be provided. The goal is to remove all elements from the initial array that have the same value as these arguments.
I need help understanding why the code snippet below is not removing the necessary elements for the second test case.
function destroyer(arr) {
console.log(arguments);
for (var array_i = 0; array_i < arr.length; array_i++){
for (var arg_i = 1; arg_i < arguments.length; arg_i++){
console.log("indexes", array_i, arg_i);
if (arr[array_i] === arguments[arg_i]){
console.log(arr[array_i], arguments[arg_i], "destroyed");
arr.splice(array_i, 1);
console.log(arr, arguments);
array_i = 0;
arg_i = 1;
}
}
}
return arr;
}
The code works as expected in this scenario:
destroyer([3, 5, 1, 2, 2], 2, 3, 5);
However, it fails to produce the desired outcome in this case:
destroyer([2, 3, 2, 3], 2, 3);