NaN testing Trouble

NaN testing Trouble
0

#1

Hello,

I am currently doing the Falsy Bouncer algorithm challenge and in need of some help.

I am stuck on how to test for the value NaN. The problem is that when I run it through the function isNaN(), it also returns regular strings as true.

Any advice you have will be helpful.

Thanks!!!


#2

isNaN processes strings this way: Let’s take isNaN('hello') as an example. First isNaN parses 'hello' as a number (like using Number('hello')), and since, hello cannot be parsed as a number, the parse attempt returns NaN. It’s this NaN the the isNaN checks, and therefore isNaN('hello') returns true.

You may want to use Number.isNaN() instead. It will return false as expected when a string is passed to it.

Read more here: isNaN and Number.isNaN


#3

NaN is falsy.


#4

There is no need to test every possible “falsy” condition. Simply check for truthiness. Here’s the function to do it:

function isTruthy(x) { return x; }

This function is typically also called identity or just id