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

160
Visualizações
How to exit nested while loop
var userName = input.question('Please enter your name: ');
console.log('\nHi ' + userName + ', please select your choice:');
var choice = input.question('1. Display Name 2. Display Age');
while (choice == 1 {
 console.log ("Hello");
 var choice = input.questionFloat('\1. Display Name 2. Display Age');
}
while (choice == 2 {
 console.log ("Hello");
  var subChoice = input.question('1. Display date 2. Display time');
  while (subChoice == 1) {
             console.log ("Hello");
  }
  while (subChoice == 2) {
             console.log('\nHi ' + userName + ', please select your choice:');
             var choice = input.questionFloat('\1. Display Name 2. Display Age');
  }
}

The first block of code is asking the user their name, after which the user will be required to enter either choice 1 or 2.

The choice that the user chose will be stored in choice. I have both choice as well as subChoice. subChoice comes after the user selects choice. In this case, in order to access the subChoice 1 or 2, the user will have to select choice 2 first. If I want to exit the subChoice, I would have to select subChoice 2 which will bring me back to the choices for choice 1 & 2.

However, the problem that I am facing is that after returning to choice 1 or 2, the variable userName does not contain a value or string any more. In addition, when I select other choices i.e choice 1, it does not do anything but re-prompting me the same thing with no userName and loops in the subChoice == 2 itself.

Is there any solution to this?

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Use if structure agains while loop. The closing parenteses was missing almost every while conditions. You used var agains let, and you redeclared the choice variable multiple times.

I cannot run the code here, but somthing like this should work:

let userName = input.question('Please enter your name: ');
console.log('\nHi ' + userName + ', please select your choice:');
let choice = input.question('1. Display Name 2. Display Age');

if (choice == 1) {
  console.log ("Hello");
  choice = input.questionFloat('\1. Display Name 2. Display Age');
}
if (choice == 2) {
  console.log ("Hello");
  let subChoice = input.question('1. Display date 2. Display time');
  if (subChoice == 1) {
   console.log ("Hello");
  }
  if (subChoice == 2) {
   console.log('\nHi ' + userName + ', please select your choice:');
   choice = input.questionFloat('\1. Display Name 2. Display Age');
  }
}

about 4 years ago · Santiago Gelvez 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