Why doesn't splice function remove "false"?

Tell us what’s happening:

Hi, I have tried to make a code that would convert each array item to boolean and check if it’s false and if it is, it would remove it from the array but for some reason, it doesn’t remove “false” value.

Your code so far


function bouncer(arr) {
let y = []
for(let i of arr){
  if(Boolean(i)==false){
    arr.splice(arr.indexOf(i),1)
  }
}
console.log(arr)
}
bouncer([7, "ate", "", false, 9]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.

Challenge: Falsy Bouncer

Link to the challenge:

Hi there, I don’t this it actually iterate through all the members of the array. If you don’t want to use a cycle maybe use foreach method, something like this:

var numbers = [65, 44, 12, 4];
numbers.forEach(myFunction)

instead of myFunction use bouncer function and remove the for.

Hi,

  1. Try new Boolean instead of Boolean
  2. I would not take out the wrong items from the array, I would make a new one and stick the right items into them.

If you start splicing an arr you remove elements from it. So, if you have
arr = [1, 2, 3, 4, 5] and you say
arr.splice(0, 1) -> you get [2, 3, 4, 5] but when you are iterating through an arr you won’t start at the zero index but at the next one. Right? At three and not at two.

Greets,
Karin