# A request for help with the basic algorithm scripting exercise 'falsy bouncer'

Hi folks,

I am working through the basic algorithm scripting and have hit an odd snag with ‘falsy bouncer’.

I chose to do this by using arr.filter(fn) - there are other ways but I wanted to do this. So I created my function filter(n) and this is it:

``````function filter(n) {
if (n === false || n === null || n === 0 || n === "" || n === undefined || n === NaN){
return false
}else{
return true
}

}
``````

I also created the main function which calls this. I haven’t included it because it works perfectly and I don’t want to spoil things for you.

My problem is this…

filter(n) works perfectly, returning false for false, null, 0, “”, and undefined, correctly returning false and true for anything else. But, it returns true for NaN. I have tried putting NaN in as a string, as a list, as an object, I’ve put it in its own if statement, but whatever I do it steadfastly returns true. And I notice that in the code here it is being treated differently to all the others. I’m stuck on this one and could do with a bit of a pointer if one is available.

Cheers folks,

Bill

2 Likes

Update:

I have managed to solve this with a considerable amount of reading around the subject. I now know there are much simpler ways of doing this, but I solved it with the code I wrote - that is my level in javaScript - some of the less code intense versions of the solution (like 4 lines of code) leave me not understanding how they actually work.

For anyone who needs to know I used !!n === !!false etc - yes, the double negative works with NaN.

2 Likes

I don’t know wich exercise you were you working on but you might like to try this code:

`function f(n) { if(!n) return false; return true; }`

wich it checks if n is not a truthy value then it will return false, else true.

Of course you also could write this way too:

`function f(n) { if(n) return true; return false; }`

It is the same but checking if is truthy first.

You might like to read more about it in MDN: truthy and falsy values.

I hope this helps you to understand. Well done anyway!!

1 Like

I guess I checked whether the items in the array are truthy - at least I can’t remember having aproblem with NaN

Hopefully I remember this when I’ll need it somewhen.
Probably you won’t forget it that fast since you investigated a lot

1 Like

Thanks Carlos, I did actually sort it out in the end and I will go back to it later and work out an easier way of dealing with it.

2 Likes

Thanks Tzerio, this idea of truthy and falsy is a little odd to me but I know where to go to get the information and I guess I will keep trying until I really understand it.

1 Like

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