Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
var x=prompt("enter age here", "10");
if(x>60)
{
document.write("older");
}
else if(x>40)
{
document.write('Medium age');
}
else if(x>18)
{
document.write('young');
}
else if (x>0)
{
document.write('child');
}
else {
alert("invalid input");
}

What I have tried:

if i am entering a number more than 60 even any number like 200,300,2000,2000000, getting answer is 'older'. Can anybody tell me how to validate it i mean age should not be more than 120 years.
Posted
Updated 21-Feb-18 0:53am
v2

Just add another "if"

if (x>120)
{
document.write("too old");
}
else if(x>60)
{
document.write("older");
}
 
Share this answer
 
Comments
CPallini 21-Feb-18 6:49am    
Sometimes is sooooooooooooooooo simple. :-D
5.
Maciej Los 21-Feb-18 7:20am    
5ed!
It's because your code is written that way. You have asked your program to say older when x is greater than 60.

Try something like following-
JavaScript
var x=prompt("enter age here", "10");
if(x>60 && x<=120)
{
document.write("older");
}
//...rest of the code as it is.


Hope, it helps :)
 
Share this answer
 
Comments
Maciej Los 21-Feb-18 7:21am    
5ed!
Member 13688822 21-Feb-18 7:33am    
thanks
Suvendu Shekhar Giri 21-Feb-18 7:41am    
Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900