SyntaxError:意外的令牌案例(我无法找到它)

时间:2014-07-08 20:50:56

标签: javascript

我无法弄清楚在编写此代码时我犯了什么错误。我在Java上还是非常新的;但我经过几次梳理,我找不到任何东西。 任何人都可以找出错误的原因吗?

var user = prompt("Welcome to Hogwarts! The sorting hat is on your head; are you brave,      smart, nice, or cunning?").toLowerCase();
switch(user) {
case 'brave':
    console.log("You were sorted into Gryffindor!");
    var gryffindor = prompt("Would you like to go to the common room, dormitory or the lake?").toLowerCase();
    if (gryffindor === 'common room' || gryffindor === 'dormitory') {
        console.log("After being given the password by a prefect, you tell it to the Fat Lady. Inside the Gryffindor Tower, you meet lots of new people!");
    } else {
        console.log("You go down to the lake, when you meet a Ravenclaw you met on the train. He's trying to show off.");
        var brave = prompt("Do you want to show off or ignore him?").toLowerCase();
        switch(brave) {
            case 'show off':
                var win = Math.random();
                if (win > 0.5) {
                console.log("You try doing the spell he's doing, but you don't do very well. Oh well, maybe being foolhardy isn't the best.");
                } else {
                console.log("You do another, more complicated spell that he can't do! Take that, pompous people!");
                }
                break;
            case 'ignore him':
                console.log("You walk away and find some new friends to talk with.");
                break;
            default:
                console.log("Hm, I didn't recognize what you said.");
    }
    break;
case 'smart':
    console.log("You were sorted into Ravenclaw!");
    var ravenclaw = prompt("Would you like to go to the common room or down to the lake?").toLowerCase();
    if (ravenclaw === 'common room') {
        console.log("You use the eagle knocker to knock on the door, and it gives you a riddle. You eagerly answer the riddle; you've heard much about Ravenclaw Tower.");
        var riddle = Math.random();
        if (riddle > 0.7) {
            console.log("You got the riddle wrong!");
        } else {
            console.log("You got the riddle right! You enter the common room and see hordes of intelligent, like-minded people. This is where you belong.");
        }
    } else {
        console.log("You go down to the lake, where you talk with some other first-years about the biology of the giant squid in the lake.");
    }
    break;
case 'nice':
    console.log("You were sorted into Hufflepuff!");
    console.log("You go to the common room, where some of the older kids are throwing a party. All the other Hufflepuffs are great, and you quickly make new friends.");
    break;
case 'cunning':
    console.log("You were sorted into Slytherin!");
    var snake = prompt("Do you want to go to the common room?)"
    var slytherin = prompt("You see another first year, Draco Malfoy walking up to the common room. He seems quite condescending towards others; do you want to talk to him?").toLowerCase();
    if (slytherin === 'yes' && snake === 'yes') {
        console.log("You start talking, but you want to leave the conversation almost immediately after its start. People like Draco are the reason Slytherin house has a bad name.");
    } else {
        console.log("You see Draco while walking and almost immediately turn in the opposite direction. Maybe it's best for you to go to the lake for now.");
    }
    break;
default:
    console.log("Hm, I didn't quite understand that. Maybe try again?");
};

好的,我明白了。非常感谢!

2 个答案:

答案 0 :(得分:1)

您需要在}之后关闭console.log("Hm, I didn't recognize what you said.");,并且需要修改此common room?)"common room?")

的引号

工作演示:http://jsfiddle.net/seancannon/ZQRBn/

答案 1 :(得分:0)

需要进行2项更改。第26行,开关的闭合支架丢失,第52行。

// Code goes here

var user = prompt("Welcome to Hogwarts! The sorting hat is on your head; are you brave,      smart, nice, or cunning?").toLowerCase();
switch(user) {
case 'brave':
    console.log("You were sorted into Gryffindor!");
    var gryffindor = prompt("Would you like to go to the common room, dormitory or the lake?").toLowerCase();
    if (gryffindor === 'common room' || gryffindor === 'dormitory') {
        console.log("After being given the password by a prefect, you tell it to the Fat Lady. Inside the Gryffindor Tower, you meet lots of new people!");
    } else {
        console.log("You go down to the lake, when you meet a Ravenclaw you met on the train. He's trying to show off.");
        var brave = prompt("Do you want to show off or ignore him?").toLowerCase();
        switch(brave) {
            case 'show off':
                var win = Math.random();
                if (win > 0.5) {
                console.log("You try doing the spell he's doing, but you don't do very well. Oh well, maybe being foolhardy isn't the best.");
                } else {
                console.log("You do another, more complicated spell that he can't do! Take that, pompous people!");
                }
                break;
            case 'ignore him':
                console.log("You walk away and find some new friends to talk with.");
                break;
            default:
                console.log("Hm, I didn't recognize what you said.");

        }
    }
    break;
case 'smart':
    console.log("You were sorted into Ravenclaw!");
    var ravenclaw = prompt("Would you like to go to the common room or down to the lake?").toLowerCase();
    if (ravenclaw === 'common room') {
        console.log("You use the eagle knocker to knock on the door, and it gives you a riddle. You eagerly answer the riddle; you've heard much about Ravenclaw Tower.");
        var riddle = Math.random();
        if (riddle > 0.7) {
            console.log("You got the riddle wrong!");
        } else {
            console.log("You got the riddle right! You enter the common room and see hordes of intelligent, like-minded people. This is where you belong.");
        }
    } else {
        console.log("You go down to the lake, where you talk with some other first-years about the biology of the giant squid in the lake.");
    }
    break;
case 'nice':
    console.log("You were sorted into Hufflepuff!");
    console.log("You go to the common room, where some of the older kids are throwing a party. All the other Hufflepuffs are great, and you quickly make new friends.");
    break;
case 'cunning':
    console.log("You were sorted into Slytherin!");
    var snake = prompt("Do you want to go to the common room?");
    var slytherin = prompt("You see another first year, Draco Malfoy walking up to the common room. He seems quite condescending towards others; do you want to talk to him?").toLowerCase();
    if (slytherin === 'yes' && snake === 'yes') {
        console.log("You start talking, but you want to leave the conversation almost immediately after its start. People like Draco are the reason Slytherin house has a bad name.");
    } else {
        console.log("You see Draco while walking and almost immediately turn in the opposite direction. Maybe it's best for you to go to the lake for now.");
    }
    break;
default:
    console.log("Hm, I didn't quite understand that. Maybe try again?");
};