When I want to re-execute previous command


#1

var user = prompt("Do you wanna play a game? YES/NO or Maybe").toUpperCase();

switch(user)
{
case 'YES' :
console.log("then, Let's play the game.");
break;
case 'NO' :
console.log("You have already been trapped, you cannot escape.");
break;
case 'MAYBE' :
console.log("There is no maybe. you are playing the game.");
break;
default :
console.log();
};

for example, in this case, I want users to re-enter their answer until they enter an input within range of swtich.

I have tried to put
if statements in this way :
if(user === "YES")
{
console.log(switch);
}
else if(user === "NO")
{
console.log(switch);
}
else if(user === "MAYBE")
{
console.log(switch);
}
else
{
console.log(prompt(user));
};

but the page says there are errors within syntax.
How do I do this? and can someone explain why this does not work?
Do I have to make var user to function in order to allow it to re-execute?


#2

do while loop?

var invalidInput = true;
do{
    switch(...){
        [...]
        case ...: 
            ...
            invalidInput = false;
            break;
    }
}while;

#3

like how? I don't really understand what you are trying to say with while


#4

You wrap your switch case in a do while loop if it finishes as expected you change the value of invalidInput or whatever you like it to false and it will not start looping. And if you want to execute it again you leave it unchanged then the loop will do the re-execution.