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

238
Views
how to use while loop on a prompt in Javascript

[

  let welcome;
  welcome = "Welcome to my first COMP1231 Program.";
  alert(welcome)

  let name;
  name =(prompt("Please enter your name:?", "Tafadzwa Marisa" ));


  let program;
  program =(prompt("Please enter your Program:?", "COMP1231" ));

]1 So l am trying to use a prompt which asks a user for their name and when the user enters a name it proceeds to another prompt but when the user doesn't enter anything it loops back to the first prompt which is ask for the users name. If the user enters a valid input it then goes to the program prompt where if the user enters any input it proceeds but if the user doesn't it loops to the same prompt. I want to use the while loop for this

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

It looks like this is a homework question or you are just trying to learn programming in general, so in order to help you progress I will not give you the code itself but a general direction. If you have problems with that you can give your code and we can try to help you with any specific errors.

There are two types of loops including the while keyword, while(X) {Y} and do {Y} while(X). The first type is useful if you don't know if the loop will be executed at all, while the second is more appropriate here as it is used when the loop should be executed at least once, which is necessary for the user to enter the name. So assign the prompt result to your name variable and then checks if the name is empty. In case the condition evaluates to true (the name is empty), it will loop back.

You could also initialize the name as an empty string and use a while(X) {Y} loop but the other form fits better here.

about 4 years ago · Juan Pablo Isaza Report

0

You can create a function which will basically execute a while loop until user enters some input. This function returns the input value if it's not empty.

function loopUntilNonEmptyInput(prompt_ques, default_inp) {
  let retval;
  while (true) {
    retval = (prompt(prompt_ques, default_inp));
    
    if (retval !== '')
      return retval;
      
    alert("Empty input, please re-try");
  }
}

let welcome;
welcome = "Welcome to my first COMP1231 Program.";
alert(welcome);

let name = loopUntilNonEmptyInput("Please enter your name:?", "Tafadzwa Marisa");
let program = loopUntilNonEmptyInput("Please enter your Program:?", "COMP1231");
console.log(name, program);

about 4 years ago · Juan Pablo Isaza Report

0

so after researching l was finally able to find the best suitable way. Thank you for you suggestions and your help. My issue was mainly on how l was arranging the year value as a string for while loop as l was using (year<1&&year>3)which was causing the error. After getting an idea on what not to put l finally figured it out

let year;
// taking year value as string
year =(prompt("Please enter your year of study:", "1"));
// loop will continue until user 1 or 2 or 3
while(parseInt(year) <1 || parseInt(year) > 3 || year=== ""){
    // again taking year as string
year = (prompt("Please enter your year of study:", "1"));
}

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!