Basic Algorithm Scripting: Falsy Bouncer - why my code isn't working?

can someone help me find the bug please ?

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

bouncer([7, "ate", "", false, 9]);

Notice that i is being incremented after the loop code being executed.

NaN behaves slightly different than one would expect when being compared like that:

console.log(NaN === NaN) // false

It needs to be checked differently.

Having said all that I’d suggest to consider two questions. How you might solve this without modifying array you are iterating over? As you see it can be tricky to prevent possibility of missing one of the elements when array is changing. How to check whether value is falsy might be done in another way? Not one by one, but all possibilities at once.

1 Like

You are making a common mistake with the Falsy Bouncer. You can make a solution for this challenge which tests against a hardcoded list of falsy values, but it misses the point of the exercise. The concept of values being “falsy” isn’t something made up by Free Code Camp for the purposes of this challenge, but rather a quality that values have in programming languages. A value is falsy if the language treats the value as false when it is evaluated as a boolean.

This means that Boolean(someFalsyValue) is false but also that we can use it in a conditional like if (someFalsyValue). We can also talk about values being “truthy”. As I’m sure you’ve guessed, truthy values evaluate to true in a boolean of logical context.

if (someFalsyValue) {
    // this code block is not entered
else if (someTruthyValue) {
    // this code block is entered and executed