are the instructions for this simple nested if statement exercise: Create a small JavaScript program that:
Prompts a user for their name.
Prompts the user for their age.
Uses an if statement to determine if a person is 18 or older.
Alerts "Hello <user>, you are an adult" if they are 18 or older.
Uses a nested if statement to check to see if adults are retired (leave the children alone)
Alerts "You are retired!" if they are 65 or over.
Alerts "Hello <user>, you are a child" if they are not 18 or older.
The output should be as follows:
Users under 18
Hello <user> you are a child
Users 18 and over
Hello <user> you are an adult
Adults 65 and over
You are retired!
So I am not sure what sort of error happened when I wrote the second if statement for the users who are above 65. I've included the syntax in the code section. It's a simple structure but I don't why the output won't properly.
<script type="text/javascript">
var Name;
var Age;
Name = prompt("Please enter your name.", "full name");
Age = parseInt(prompt("Please enter your age.", "age in numerals"));
if (Age >= 19)
{
alert("Hello " + Name + " you are an adult.");
}
if (Age >= 65)
{
alert("You are retired!");
}
else
{
alert("Hello " + Name + " you are a child.");
}
</script>
What's wrong is that it's not actually nested. You need to put the second if inside the first if block.
The way you wrote it, the else is associated with the test for 65 and older, not 18 and older.
Also, 19 should be 18.
var Name;
var Age;
Name = prompt("Please enter your name.", "full name");
Age = parseInt(prompt("Please enter your age.", "age in numerals"));
if (Age >= 18) {
alert("Hello " + Name + " you are an adult.");
if (Age >= 65) {
alert("You are retired!");
}
} else {
alert("Hello " + Name + " you are a child.");
}