while loop illegal break

时间:2016-04-04 19:00:37

标签: javascript html while-loop break

Ok I keep getting illegal break in my code while running it in chrome.

//village
var village = function () {
    document.write("<p>You come to a small village with a Smiths shop, a bakery, a guards Tower, and a bank. If you follow thst path some more you'll wind up going into the forrest.</p>")
    window.scrollTo(0, document.body.scrollHeight)
    while (choice != "bakery" || "tower" || "bank" || "forrest" || "smith") {
        var choice = prompt("bakery,tower,smith,bank,forrest")
        if (choice = "bakery") {

            villageBakery(); // its this line getting flagged, not even the bellow it. only this line.

            break;
        } else if ("tower") {
            document.write("<p>You walk up and knock on the door. No one came.</p>")
        } else if ("smith") {
            villageSmith()
            break
        } else if ("bank") {
            villageBank()
            break
        } else if (choice == "forrest") {
            forrest()
            break
        } else {
            document.write("<p>Please type the choices as shown</p>");
            window.scrollTo(0, document.body.scrollHeight)
        };
    };
};

I've tried googling the problem but I keep coming up short. I have my other functions set up the same way but its only saying this one is busted.

2 个答案:

答案 0 :(得分:0)

你需要改变条件,你的条件是否与if条件相反,这意味着如果你不会在任何内部,你可以做这样的事情:

  

while(选择===&#34;面包店&#34; ||选择===&#34;塔&#34; ||选择===&#34;银行&#34; ||   选择===&#34; forrest&#34; ||&#34;选择===史密斯&#34;){

    var choice = prompt("bakery,tower,smith,bank,forrest")
    if (choice === "bakery") {

        villageBakery(); // its this line getting flagged, not even the bellow it. only this line.

        break;
    } else if (choice ==="tower") {

希望这可以提供帮助

答案 1 :(得分:-1)

在制动声明后面加上分号:       break;