I need an explaination (Falsy Bouncer)


Why the string does not come out and How can I remove NaN?

What the exercise is trying to teach you is that JavaScript has the concept of “truthy” and “falsely” values. When it converts (coerces) a value that isn’t literally true or false to an actual true or false (a boolean), the language has rules for what it considers to be false: all of the values you are filtering out are considered false (0, false, null, undefined, NaN and an empty string '').

To fix what you have:

NaN is not equal to anything else, not even to itself. Think of it is a value that JS has been told is a number, but when JS tries to read it, it can’t understand it. As it has no idea what it actually is, it can’t say “this unknown thing is the same as this other unknown thing”.

To check, you can simply do arr[i] !== arr[i], ie this value does not equal itself.

With the string, you can literally just do arr[i] !== ''. Also you’ve misspelled string as strnig. You don’t need to do typeof, you can just check it isn’t an empty string, or isn’t 0 or isn’t null or whatever (except for NaN where you need to do what I suggested).


This solves it, but you don’t need to do any of this: if you read back the first paragraph I wrote, about how JS has truthy and falsely values, you just need to check if the value is truthy or falsey.

There are much more concise ways to do this, but probably the easiest to inderstand is a function called Boolean(). If you give it a value, it converts it to true or false - Boolean(1) returns true, Boolean(null) returns false, Boolean('hello') returns true and so on.

3 Likes