重复switch语句,直到它有一个有效的答案

Fin*_*n E 3 javascript for-loop switch-statement

我是一个尝试用JavaScript制作文本冒险游戏的初学者,我需要重复一个switch语句,直到用户输入一个有效的答案:

opts = prompt("Do you want to TALK or LOOK?").toUpperCase();

switch(opts) {
  case "TALK":
    mes = "Is anyone in charge here?";
    speech = "Our leader is in the big building.";
    talkNot();
    break;
  case "LOOK":
    window.alert("The buildings are quite simple, and the doorways are much shorter than you. You notice, however, that there is a very tall, thin building at the end of the street.");
    break;
  default:
    window.alert("That's not an option.");
}
Run Code Online (Sandbox Code Playgroud)

任何答案都会非常有用 - 谢谢!

pra*_*nth 6

用一些函数和callback函数在default语句中包装代码

function run(){
opts = prompt("Do you want to TALK or LOOK?").toUpperCase();

switch(opts) {
  case "TALK":
    mes = "Is anyone in charge here?";
    speech = "Our leader is in the big building.";
    console.log('talk')
    //talkNot();
    break;
  case "LOOK":
    window.alert("The buildings are quite simple, and the doorways are much shorter than you. You notice, however, that there is a very tall, thin building at the end of the street.");
    break;
  default:
    window.alert("That's not an option.");
    run() //callback 
}
}

run()
Run Code Online (Sandbox Code Playgroud)