The Problem is, when you do splice, you are adjusting your indexes. You could have added a simple debug statement like console.log to identify whats happening with your code.
I just added a sample stdout line before doing validation
function bouncer(arr) {
for(let i=0; i<arr.length; i++){
console.log(arr[i]);
if(!arr[i]){
arr.splice(i, 1)
}
}
return arr;
}
console.log(bouncer([7, "ate", "", false, 9]));
And your code does print as below
[/private/tmp]$ node sto.js
7
ate
9
[ 7, 'ate', false, 9 ]
So if you observe, after "" string, as you have used splice the index got adjusted and false missed from iteration.
Solutions
- Use another array and push all non invalid items into it.
- Use filter which does same for you automatically.