Unexpected token case


#1

It told me that my code had an "unexpected token case". I can't seen to find it though. My code is a bit long so I would really appreciate your help in finding what went wrong with my code. Thank you.

var user = prompt("You are crossing a dangerous and unstable bridge. A giant bird suddenly lands on it and the bridge shakes violently. It asks you if you have any food. Do you answer it, try to shoo it away, or ignore it and run across the bridge?").toLowerCase();

switch (user)
case 'answer it':

    var answer = prompt("Do you say yes or no?").toLowerCase();
    if (answer === 'yes') {
        var food = prompt("What food do you have?").toLowerCase();
    } else {
        alert("You do not have any food for the bird so it eats you instead.");
    };

    break;

case 'try to shoo it away':
    var shoo = prompt("Do you ask it politely or try to hit the bird").toLowerCase();


    if (shoo === "ask it politely") {
        alert("The bird tells you give it food or it will eat you")
        var eat = prompt("Do you give it food poisoning, all of your money, or try to hit it.").toLowerCase()
        if (eat === "give it food poisoning") {
            alert("You killed the bird but it's dead body falls onto the bridge and the bridge falls down dragging you along with it");
        } else if (eat === "all of your money") {
            alert("Although annoyed that it has to go shopping, the bird is satisfyed with the money and holds the bridge long enough for you to cross without it breaking.");
        } else if (eat === "try to hit it") {
            var slaying = true;
            var youHit = Math.floor(Math.random() * 2);
            var damageThisRound = Math.floor(Math.random() * 5 + 1);
            var totalDamage = 0;

            while (slaying) {
                if (youHit) {
                    console.log("You hit the bird and did " + damageThisRound +
                        " damage");
                    totalDamage += damageThisRound;

                    if (totalDamage >= 4) {
                        console.log("The bird is dead and you push it's body off of the bridge before it falls onto the bridge and breaks it.");
                        slaying = false;
                    } else {
                        youHit = Math.floor(Math.random() * 2);
                    }

                } else {
                    console.log("The bird killed you ");
                    slaying = false;
                }
            };

        };

    };


    if (shoo === "try to hit the bird") {
        var slaying = true;
        var youHit = Math.floor(Math.random() * 2);
        var damageThisRound = Math.floor(Math.random() * 5 + 1);
        var totalDamage = 0;

        while (slaying) {
            if (youHit) {
                console.log("You hit the bird and did " + damageThisRound +
                    " damage");
                totalDamage += damageThisRound;

                if (totalDamage >= 4) {
                    console.log("The bird is dead and you push it's body off of the bridge before it falls onto the bridge and breaks it.");
                    slaying = false;
                } else {
                    youHit = Math.floor(Math.random() * 2);
                }

            } else {
                console.log("The bird killed you ");
                slaying = false;
            }
        };

    };

    break;

case "ignore it and run across the bridge":
    var run = prompt("Do you run fast, slightly fast, or slowly").toLowerCase();

    if (run === "fast") {
        alert("You ran too fast that you shook the bridge too much and it collapsed");
    } else if (run === "slightly fast") {

        alert("The bird catches up to you but your headstart give you enough time to react to it's attack. You now are forced to battle the menacing and angry bird.");
        var slaying = true;
        var youHit = Math.floor(Math.random() * 2);
        var damageThisRound = Math.floor(Math.random() * 5 + 1);
        var totalDamage = 0;

        while (slaying) {
            if (youHit) {
                console.log("You hit the bird and did " + damageThisRound +
                    " damage");
                totalDamage += damageThisRound;

                if (totalDamage >= 4) {
                    console.log("The bird is dead and you push it's body off of the bridge before it falls onto the bridge and breaks it.");
                    slaying = false;
                } else {
                    youHit = Math.floor(Math.random() * 2);
                }

            } else {
                console.log("The bird killed you ");
                slaying = false;
            }
        };
    } else if (run === "slow") {

        alert("You ran so slow that the bird caught up to you and killed you from the behind.")
    };

    break;

default:

    alert("Really? You're not going to do anything? I mean if you don't at least do something then the bird will eat you. That bird is one hungry bird!");


#2

I have formatted your code using jsbeautifier and now the problem must be apparent.

Hint Something's missing on the third-fourth line :wink:


#3

I feel so stupid now. I can't believe I forgot the curly brackets. THANK YOU SO MUCH! :smile:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.