Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
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>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.");
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!