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

237
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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