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

103
Visualizações
Where am I going wrong with my while loop?

I am new to how loops work. I want the loop to end if the input is N, for "Do you play minecraft?" If the user inputs Y, after the user inputs how many hours, I need it to continue to the calculation part and have it display. My program does not do that. I'm not sure why it continues looping and doesn't go on to the next part.

//Variables
var hours;
var total;
var keepLooping = "Y";
var askingAQuestion = true;
userInput = "";

//calculation and conversion
total = hours * 7;
hours = Number(hours);

// Get the input from the user
function myFunction() {
    while (keepLooping === "Y") {
        userInput = prompt("Do you play minecraft?" + " Y or N");
        if (userInput === "N") {
            askingAQuestion = false;
            document.write("You should download the free trial!");
        } else if (userInput === "Y") {
            document.write("That's great!");
            hours = prompt("how many hours a day do you play minecraft?")
        } else if (hours >= 3) {
            document.write("if You play minecraft" + hours + "a day");
            document.write("You play minecraft" + total + "hours a week!");
        }

    }
}

myFunction();

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

0

Change the keepLooping value when meet conditions

//Variables
var hours;
var total;
var keepLooping = "Y";
var askingAQuestion = true;
userInput = "";

//calculation and conversion
total = hours * 7;
hours = Number(hours);

// Get the input from the user
function myFunction() {
    while (keepLooping === "Y") {
        userInput = prompt("Do you play minecraft?" + " Y or N");
        if (userInput === "N") {
            askingAQuestion = false;
            keepLooping =  "N";
            document.write("You should download the free trial!");
        } else if (userInput === "Y") {
            document.write("That's great!");
            hours = prompt("how many hours a day do you play minecraft?")
        } else if (hours >= 3) {
            document.write("if You play minecraft" + hours + "a day");
            document.write("You play minecraft" + total + "hours a week!");
        }

    }
}

myFunction();
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