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

102
Views
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 answers
Answer question

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 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!