Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda