拼写错误后如何返回最后一个提示?

时间:2015-10-28 00:58:58

标签: javascript switch-statement prompt

所以我试图创建一个基于文本的游戏。问题是,当有人拼写错误时,它总是会通过"默认"结束游戏。我必须发出警报的条款。当有人输入不正确的答案时,有没有办法让它回到caveAnswer提示?

var caveAnswer = prompt("you are in the cave", "type GO or EXIT").toUpperCase();
        switch(caveAnswer){
            case "GO":
                prompt("some text...", "type blabla");
                break;
            case "EXIT":
                alert("COWARD! HAHAHA!");
                break;
            default:
                alert('I dont understand ' + caveAnswer);
                break;
        }

1 个答案:

答案 0 :(得分:0)

包含一个函数并让default大小写调用本身再次显示提示。

function ask() {
    var caveAnswer = prompt("you are in the cave", "type GO or EXIT").toUpperCase();

    switch(caveAnswer){
        case "GO":
            prompt("some text...", "type blabla");
            break;
        case "EXIT":
            alert("COWARD! HAHAHA!");
            break;
        default:
            alert('I dont understand ' + caveAnswer);
            ask();
            break;
    }
}

ask();