Why does my code not work for this problem? [**Challenge:** Falsy Bouncer]

Tell us what’s happening:
For this particular example, the program returns [7, “ate”, “”, 9] instead of [7, “ate”, “”, 9]. How come my code does not slice false even through Boolean(false) should equal false?

Your code so far


function bouncer(arr) {
for (var i = 0; i < arr.length; i++) {
  if (Boolean(arr[i])) {
    arr.splice(i, 1);
  }
}
return 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/85.0.4183.102 Safari/537.36.

Challenge: Falsy Bouncer

Link to the challenge:

Welcome to the forum!

Splice changes the contents of the array as you iterate. This will make a mess of your loop. I would instead make a new array with only the truthy values.

1 Like