mardi 5 octobre 2021

What is wrong with this Nested If syntax?

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>

Aucun commentaire:

Enregistrer un commentaire