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

99
Visualizações
Display second prompt if the first prompt is not TRUE

I have a problem when i try to display new prompt, i use loop but it will display both my prompt. Here is my code and i really need help

<body>
<p id="age" style="font-weight: bold"></p>
<button onclick=" notify()">Try it</button>
<script>
function notify(){
var age = document.getElementById("age").value;
var age = prompt("Enter Your birth year:");// This is the first prompt 
const year = new Date().getFullYear();
if (age != null) { 
          document.getElementById("age").innerHTML =
            "Your " + age + " is so beautiful"; // If user enter birth year < current year will display this
        }
  do {
     age = prompt("Re-enter your birth year:"); // Ortherwise, this will display and they need to enter until birth year < current year
    } while (age > year && age != null);
        var tuoi = year - age;// This is just calculate user'age, for example if the user enter 2000 will display user'age is 22
        document.getElementById("age").innerHTML =
          "Tuổi " + tuoi + " này đẹp đó";
}
</script>
</body>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

your code has several issues and does some things which do not make total sense to me, but I think you want something like this:

<body>
  <p id="age" style="font-weight: bold"></p>
  <button onclick=" notify()">Try it</button>
  <script>
    function notify() {
      const year = new Date().getFullYear();
      var birth_year = parseInt(prompt("Enter Your birth year:")); // This is the first prompt
      
      if (!isNaN(birth_year) && birth_year < year) {
        document.getElementById("age").innerHTML =
          "Your " + birth_year + " is so beautiful"; // If user enter birth year < current year will display this
      } else {
        do {
          birth_year = parseInt(prompt("Enter Your birth year:")); // Ortherwise, this will display and they need to enter until birth year < current year
        } while (isNaN(parseInt(birth_year)) || birth_year >= year);
        var tuoi = year - age; // This is just calculate user'age, for example if the user enter 2000 will display user'age is 22
        document.getElementById("age").innerHTML =
          "Tuổi " + birth_year + " này đẹp đó";
      }
    }

  </script>
</body>
about 4 years ago · Juan Pablo Isaza Relatório

0

You simply forgot a part of your if condition.

Change if (age != null) to if (age != null && age < year) and it will work as you expect it. Don't forget the case if age == year.

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