+ 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" ); }
18 Réponses
+ 8
Sorry, I forgot! Here's how to check for NaN:
if (isNaN(age))
+ 6
Check for an empty string instead:
else if (age === "")
+ 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.
+ 1
You want the program to say "Enter a valid number" when the user types an input that is not a number?
+ 1
It doesn't work too. Please test your code before posting here. I'm starting to think that you're fooling me
+ 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
0
No, it doesn't work
0
still not working :(
0
What is isNaN defined as?
0
mistake1:age>=21;mistake2:(age==isNaN(age)||age==null)
you try it
0
Tested it. Nothing changed
0
my language is C#.....i think i'm not good for help......
0
So your problem is that it doesn't work when you input a blank prompt or letters?
0
yes
0
That's because you've programmed it to where it only accepts numerical inputs
- 1
Check if you have defined all your variables
- 1
else if (age==(age)||age==null)
- 1
Why I see JS questions under python course?