Falsy Bouncer.9

Falsy Bouncer.9
0

#1

How is that not working??

Your code so far


function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  for(let i =0;i<arr.length;i++){
    if(!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/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/falsy-bouncer


#2

Hello @msafieldeen

You’d better use filter() which will give you the ability to remove all falsy values in only one line!


#3

The reason is when ever you splice an element out, you are still increment i via the i++. This causes you to skip an element. You need to adjust i in your for loop when a splice takes place.


#4

But if you want to continue using this approach.
You’d better create a new array outside the for loop, right before it.

And then try to push into it elements that are not false.

When you return it, you will see that beautiful green window appearing on your screen.


#5

solved it man thank you . added a decrement after the splice code.


#6

Yeah , what’s the use of that screen if there was no struggle behind it xD , Thanks man i solved it anyway with a decrement