My solution for falsy bouncer

Tell us what’s happening:
Describe your issue in detail here.
My code look legit but it dont working, i tired put in visualizer it cant detect false may i know why?

  **Your code so far**
function bouncer(arr) 
{
for (let i = 0; i < arr.length; i ++)
{
    if(arr[i] === false || arr[i] === null || arr[i] === "" || arr[i] === undefined || arr[i] === NaN || arr[i] === 0)
    {
        arr.splice(i,1);
    }
}
return arr;
}

bouncer([7, "ate", "", false, 9]);
  **Your browser information:**

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

Challenge: Falsy Bouncer

Link to the challenge:

congrats! If you passed the challenge. your code has some issues it might help you in future challenges.

there is a space between i and ++ and splice is reducing arr size but your for loop does not know about it, may be array out of bound error occur

oops that a mistake but i fixed it still cant working , anyway thanks for telling me the mistake

Two things.

This lesson is trying to drive home that the condition in if statements is checking truthiness already. You do not need to check for every possible falsy value in JavaScript.

Second, splice mutates the array that you’re looping over. This is bad, because the loop termination depends on the i value and the array’s length, but the length keeps changing. A better approach is to do the opposite, build up a new array of values you want to keep, and return that new array at the end.

1 Like

third, NaN === NaN is false

1 Like

so thats why it doesnt execute the code because have two false in the statement?

what do you mean that it doesn’t execute? are you getting a syntax error?

i dont understand what you mean by Nan === Nan is false

Literally, NaN === NaN is not true, it is false. You can’t check if a value is NaN by using the strict equality operator.

I see … i get it now thanks!