+ 3

Can you help me with the program?

Hello everyone! I've just started to learn JS course and have the problem with if starement. My program works fine until I pressing ok with blank prompt or writiting a letter instead of number the "else if (age === isNaN(age) || age === null)" statemnt doesn't work. Can you help me, please? var age = prompt("Please enter your age", ""); if (age < 18) { alert("You can't come in"); } else if (age >= 18 && age < 21) { alert("You can come in but can't drink"); } else if (age => 21) { alert("You can come in and drink"); } else if (age === isNaN(age) || age === null) { alert("Enter a valid number" ); }

25th Oct 2017, 10:52 PM
selipasha
selipasha - avatar
18 Answers
+ 8
Sorry, I forgot! Here's how to check for NaN: if (isNaN(age))
25th Oct 2017, 11:30 PM
Tamra
Tamra - avatar
+ 6
Check for an empty string instead: else if (age === "")
25th Oct 2017, 11:13 PM
Tamra
Tamra - avatar
+ 3
You can't say if(age===isNaN(age)). isNaN as a function is going to return a boolean (true or false). So simply say if(isNaN(age)). Otherwise you will always get false when comparing to age unless the user enters true for the prompt. I don't know about the null entry.
2nd Nov 2017, 11:44 AM
Mark McGhee
Mark McGhee - avatar
+ 1
You want the program to say "Enter a valid number" when the user types an input that is not a number?
25th Oct 2017, 11:48 PM
jacksonofgames 28
jacksonofgames 28 - avatar
+ 1
It doesn't work too. Please test your code before posting here. I'm starting to think that you're fooling me
27th Oct 2017, 3:40 AM
selipasha
selipasha - avatar
+ 1
I think best way is to nest your if statements in another if (age>0)...else...enter valid number var age= prompt("Please enter your age", ""); if(age>0){ if (age < 18) { alert("You can't come in"); } else if (age >= 18 && age < 21) { alert("You can come in but can't drink"); } else if (age => 21) { alert("You can come in and drink"); } } else { alert("Enter a valid number" ); } doing that you can avoid blank inputs and letter inputs
16th Nov 2017, 1:05 PM
ljerka
ljerka - avatar
0
No, it doesn't work
25th Oct 2017, 11:23 PM
selipasha
selipasha - avatar
0
still not working :(
25th Oct 2017, 11:46 PM
selipasha
selipasha - avatar
0
What is isNaN defined as?
25th Oct 2017, 11:57 PM
jacksonofgames 28
jacksonofgames 28 - avatar
0
mistake1:age>=21;mistake2:(age==isNaN(age)||age==null) you try it
27th Oct 2017, 6:49 AM
Chrowder
0
Tested it. Nothing changed
27th Oct 2017, 9:08 PM
selipasha
selipasha - avatar
0
my language is C#.....i think i'm not good for help......
28th Oct 2017, 12:15 AM
Chrowder
0
So your problem is that it doesn't work when you input a blank prompt or letters?
28th Oct 2017, 12:27 AM
jacksonofgames 28
jacksonofgames 28 - avatar
0
yes
28th Oct 2017, 5:41 AM
selipasha
selipasha - avatar
0
That's because you've programmed it to where it only accepts numerical inputs
28th Oct 2017, 1:06 PM
jacksonofgames 28
jacksonofgames 28 - avatar
- 1
Check if you have defined all your variables
25th Oct 2017, 11:36 PM
jacksonofgames 28
jacksonofgames 28 - avatar
- 1
else if (age==(age)||age==null)
27th Oct 2017, 12:39 AM
Chrowder
- 1
Why I see JS questions under python course?
3rd Nov 2017, 10:54 PM
George_Falconer
George_Falconer - avatar