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

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

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