I got stuck by the "Falsy Bouncer" challenge

I got stuck by the "Falsy Bouncer" challenge
0

#1

I don’t want to use “filter()” function to complete the challenge, so I wrote code in another way to solve the problem, but I got stuck.
here is my code:

/***********************************************************************/
function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  var newArr = [];
  for(var j = 0; j < arr.length;j++){
    newArr.push(arr[j]);
  }
  var sign =0;
  for (var i = 0; i < arr.length; i++ ){
    console.log("i= "+i);
    console.log("sing= " + sign);
    console.log("length= "+arr.length);
    switch(arr[i]){
      case false:
        newArr.splice(i-sign,1);
        sign ++;
        break;
      case null:
        newArr.splice(i-sign,1);
        console.log("nullnull");
        sign ++;
        break;
      case 0:
        newArr.splice(i-sign,1);
        sign ++; 
        break;
      case "":
        newArr.splice(i-sign,1);
        sign++;
        break;
      case undefined:
        newArr.splice(i-sign,1);
        sign++;
        break;
      case isNaN(arr[i]):
        newArr.splice(i-sign,1);
        console.log("NaNNaN");
        sign++;
        break;
      default: 
        break;
    }
    
  }
  return newArr;
}

bouncer([false, null, 0, undefined, "", NaN]);
/*********************************************************************/

the result was [null] instead of [].

I have two questions:

1.why “null” was still kept in “newArr”?
2.I also tried to run the code in Chrome, but the result was different. The result in Chrome is “[NaN]”.
As a result, I was totally confused by the code.

Please give me some help! Thanks!


#2

When I run your function show above with the following call:

bouncer([false, null, 0, undefined, "", NaN]);

[NaN] is returned. which is what I expect it to be.

What browser are using to test this code?

The correct answer for this call to the function should be [] (a blank array.
Why? Because the following case does not match.

case isNaN(arr[i]):

When arr[5] is NaN, isNaN(arr[i]) evaluates to true. Since NaN is not equal to true, the code block for this case does not get executed and NaN does not get spliced out of arr.


#3

I got the result as “[null]” when I was using FreeCodeCamp to edit the code.
The link is https://www.freecodecamp.org/challenges/falsy-bouncer.

there is a screenshot of the execution


#4

Well, I see what you are talking about now for that test case. I honestly do not know why the FCC console shows [null]. I do know that the FCC console is not a true console and therefore may not always show the same as the browser’s console. I can assure you your code actually is returning [NaN] for the test case in question. You can check your browser’s console as I mentioned before to see what is being returned.


#5

You are right! I have tested my code and the result was [NaN] as you said!


#6

3 posts were split to a new topic: Falsy Bouncer - how to get rid of NaN