Having a problem I can’t figure out. My seek and destroy code passes the first, second and fifth test, but fails the 3rd and 4th
My code
function destroyer(arr) {
// Remove all the values
// isolate targets
var targets = arr.slice.call(arguments, 1);
// Establish counters
var count = targets.length;
var cycle = arr.length;
// Set outer loop
for (var i = 0; i < cycle; i++) {
// Set inner loop
for (var j = 0; j < count; j++) {
// Searching....
if (arr[i] === targets[j]) {
// Seek and destroy !
arr.splice(i,1);
}
}
}
return arr;
}
destroyer([3, 5, 1, 2, 2], 2, 3, 5);
The case of destroyer([3, 5, 1, 2, 2], 2, 3, 5)
Returns [1,2] instead of just [1]
I am sure there are 100 more slick ways to solve it, but this is the first solution that came to mind and I’m at a loss how it works for some values and not for others.