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?
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');
}
}