Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

104
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda