Falsy Bouncer - how to get rid of NaN

Falsy Bouncer - how to get rid of NaN
0.0 0

#1

same here: The code seems to miss the “null” value while finding all the others. What shell I do?

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
    
  var filtered = arr.filter(faulty);
  return filtered;
}


function faulty(item)
{ 
      
  switch (item)
    {
      case false:
      case null:
      case 0:
      case "":
      case undefined:
      case NaN:
       return false;
      default:
        return true;
    }
}

bouncer([1, null, NaN, 2, undefined]);

I got stuck by the "Falsy Bouncer" challenge
#2

Read the reply I gave the OP. It applies to your code also.

It is actually the NaN which is not removed, because the statement case: NaN is checking whether item === NaN. Whenever any value tested for equality against NaN the result is always false.


#3

Thank you but how do I get around it?


#4

This is a common misunderstanding 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
}

#5

I moved your question to a new topic, so we can focus on your code and not create extra replies on the other thread.

There are a couple of ways of solving this challenge.

  1. One way is trying to use if/else if or switch/case statements to deal try and handle each of the 5 values. You incorporate the use of a special JavaScript function like Number.IsNaN to do this.

  2. The other way uses the logic @ArielLeslie has mentioned above. Since your faulty function is pass a value which is either truthy or falsy and you know that whatever value you pass back to the filter function from the faulty function will be treated as a boolean, what is the simplest thing you could return?


#6

Thank you all. I solved the challenge but in the easy way. If you can take the time to explain why my code failed (the part that I put as a remark) I will appreciate it.

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
    
  var filtered = arr.filter(faulty);
  // filtered.filter(spaecialFaulty); -- I tried to avoid using isNaN but failed
  return filtered;
}

function faulty(item)
{ 
      
  switch (item)
    {
      case false:
      case NaN:
      case null:
      case 0:
      case "":
      case undefined:
     
       return false;
      default:
        if (isNaN(item)){
            return item;
            }
        return true;
    }
}
/* function spaecialFaulty(item)
{
  return item;
}*/ 
bouncer([1, null, NaN, 2, undefined]);

#7

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#8

So are you saying the following was the code you used but could not pass the tests?

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  filtered.filter(spaecialFaulty);// -- I tried to avoid using isNaN but failed
}

function spaecialFaulty(item)
{
  return item;
} 

If so, the code has a reference error, because there is no array named “filtered” on which to use the filter function.


#9

The “filtered” array is declared on line above the call to “spaecialFaulty”

function bouncer(arr) {
// Don’t show a false ID to this bouncer.

var filtered = arr.filter(faulty);
filtered.filter(spaecialFaulty);
return filtered;
}


#10

I thought you said your other solution was just the code marked in the comments, so I assumed that was the only code you tried in your other solution. So just to make it clear to me what you tried, can you post all of the code you tried for your other solution without any comments? I ask this, because I can not tell if you also still had the switch statement in your code as well, which could make a difference.


#11

Yes I added one more filter function but it didn’t work


#12

If you post the exact code which failed the tests, then I can tell you why it failed. I don’t want to guess which code you tried based on a general description. I already tried that earlier and my guess was incorrect.


#13

Never mind. You helped my through the challenge enough.


#14

I honestly don’t mind helping you, but that is your choice.

FYI, below is the simple solution that would have worked.

Click for Solution
function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  return arr.filter(faulty);
}

function faulty(item) {
  return item;
}