Cannot fill array with strings that are not empty

Tell us what’s happening:
I have an if statement that checks for invalid data types in a array
i have a temporary array that upon passing the if statement, it stores that valid input array data
i cannot understand why my if statement is not passing non-empty string arrays.
i have manully checked all of the if statement for string and they all return true thus the if statement is true and my temporary array should store it.
i know that my if statment is evaluting one of the conditions false because upon logging the i variable for my array position, it skips the strings alongside all the other non valid data.
i am stumped because if i google how to check for empty string, i get the if condition of =="" which i have, even if i change it to === " ", i have wrong results.
i hope it is formatted correctly
Please guide this noob.
Thank you for your time!

  **Your code so far**

function bouncer(arr) {
let temp = []
let j = 0

for (let i in arr)
  if (arr[i] != false && arr[i] !=null && !isNaN(arr[i]) && arr[i] !=undefined && arr[i] !=="")
    temp[j] = arr[i]
return temp;

bouncer([7, "ate", "", false, 9])
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0

Challenge: Falsy Bouncer

Link to the challenge:

Yeah, right off the bat, this looks odd to me:

if (arr[i] != false && arr[i] !=null && !isNaN(arr[i]) && arr[i] !=undefined && arr[i] !=="")

First of all, like ==, != can be dangerous - it can lead to unexpected results. Furthermore, this could probably be simplified to:

if (arr[i])

That will trigger for anything that isn’t one of the falsy values in JS. This is a very common pattern. It may look weird, but it translates to “if arr[i] contains a ‘real’ value”, meaning something that is either a truthy primitive or reference variable.

I haven’t had a chance to dig in yet, but that caught my eye.

As suggested, if you change your test to simply

if (arr[i])

then it will work fine.

The bug in your current if test is very nasty and hard to detect. As the code is written, it will reject any non-empty string that does not represent a number. For example, if you call

 console.log(bouncer(["one", 'two', '7', '']))

you get



[ 'one', 'two', '7' ]

The operation isNaN will work with a string too, and this is the cause of the problem in your code. For example,

!isNaN('7')  // returns true!!
!isNaN('one') //returns false but you don't want to reject 'one'
'7' == 7 // this is also true

You can use Number.isNaN instead. Or do what was suggested which is the better option.

Thank you! Number.isNaN( ) worked like a charm. i am just exploring the language JS so like i look at the answers and i understand the other better ways but i also want to try like my way and now i have learnt like about the NaN type. Thank you and to others for their time!

Ty for explaining the problem of the isNaN() function, i used number.isNaN() and it works. Thank you for explaining the if (arr[i]) method also.
Thank your time!

Just to be clear, NaN is a value, not a type. The type is still number. In JS, a number can be any integer or decimal number (within the size and precision limits of the system), Infinity, and NaN. You can also have -Infinity, and even -0.

But yeah, testing for NaN can be a pain, as mentioned.

if NaN is a number, can i display it to console?

Screen Shot 2021-06-16 at 11.08.46 AM

It works for me.

i mean the actual value, or the bits of data in memory that NaN represents. i also did typeof(NaN) and was suprised it was a number but i want to see its actual value like is it a float 9999.9999 or 0.00000 or -0.000

NaN means Not a Number
You get it when you try to convert to a number something that can’t be converted to a number
or invalid maths operatiorns, for example 0/0

it’s a number but it does not have a specific numeric value
Also, it has this interesting property

It is none of those. It is a value that JS holds. I don’t know that it is specified exactly what that value represents in terms of 1s and 0s. I don’t know how it is represented and to be honest the exact implementation may depend on the JS engine - in my understanding, the ES standard defines the behavior, not the implementation. JavaScript is a high level language where we don’t have to think about the aspects of what is happening on a low level. I think it is more useful in a language like JS to just realize that there is a type called “number” and one of the values it can hold is NaN, meaning that what we set as its value could not be interpreted as a number by JS. There are certain set “not an integer or a floating point” values that JS can set a number to, including NaN, Infinity, and `-Infinity’. We don’t really care about how that is implemented, just how to use them. I assume that they are just flags on the value or something. But no, they aren’t actual values, I wouldn’t think, not the kind you are thinking of.

For that matter, how a computer stores numbers - especially floating point numbers - can be an interesting topic.

But again, you don’t really need to know the implementation details to be an amazing JS developer. To be honest, I’m not sure if you’d ever need to know, unless you were writing your own JS engine.

thank you for that detailed explination, the flags example of how NaN might be implemented has satisfied my curiosity for now!
Thank You!!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.