Hello, I run the code in my browse console, it renturns the [NaN], does the NaN belong to the falsy? Is something wrong in my code? Could someone help me?
Your code so far
function bouncer(arr) {
var falsy=[false, null, 0, "", undefined, NaN];
var newArr=[];
// Don't show a false ID to this bouncer.
for (var i=0; i<arr.length; i++){
if(falsy.indexOf(arr[i])===-1){newArr.push(arr[i])
}
}
return newArr;
}
bouncer([false, null, 0, NaN, undefined, ""]);
[NaN]
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5712.400 QQBrowser/10.2.1957.400
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/falsy-bouncer/
NaN is a falsy value, but you can not directly compare it to NaN to check for equality which is what the indexOf attempts to do. For example, if I write the following expression, it evaluates to false
NaN === NaN // false
Why? Because that is the way JavaScript is designed. You could think of it in this way. Letβs say you have an apple and an orange. I can say and apple is not a banana and I can say an orange is not a banana, but that does not mean because both of them are not bananas that an apple is an orange, In terms of NaN. If variable x is NaN (not a number) and variable y is NaN (not a number), that does not mean x and y are equal.
You could research Number.isNaN() to help solve this challenge OR you could realize that if statements will evaluate anything inside the ( ) as true or false. For example, if I write:
if (1) {
The above will evaluate to true. Why? Because 1 is a truthy value (it is not false, null, 0, ββ, undefined, or NaN]).
If I write;
if (undefined) {
The above will evaluate to false, because undefined is a falsy value.
Armed with this new knowledge, see if you can solve this challenge now.
1 Like
Thank you ,sir. I got it from your example! Not a Number===Not a Number// false