I am facing an issue with my program that is designed to eliminate items from a list of arguments.
function destroyer(arr) {
var args = [].slice.call(arr);
var data = args.shift();
for(var i = 0; i < args.length; i++){
var j = 0;
while(j < data.length){
if(args[i] == data[j]){
data.splice(j,1);
j = 0;
}
else{
j += 1;
}
}
}
return data;
}
Even though the expected output of destroyer([1, 2, 3, 1, 2, 3], 2, 3) should be [1,1], I'm only getting '1' as a response. I have tested the function outside the loop and it's returning the correct array, so I'm confused about why it's not working within the loop.
After making some adjustments, here is the updated version:
function destroyer(arr) {
var args = [].slice.call(arguments);
var data = args[0];
args.shift();
for(var i = 0; i < args.length; i++){
var j = 0;
while(j < data.length){
if(args[i] == data[j]){
data.splice(j,1);
j = 0;
}
else{
j += 1;
}
}
}
// Remove all the values
return data;
}