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

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

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