SyntaxError:意外的令牌案例?

时间:2014-06-28 03:31:43

标签: javascript switch-statement

有五种情况被接受。仍然是Java的新手,所以这是一个有点奇怪的情况。据我所知(我已经阅读了至少两次代码)它应该可以正常工作,除非双开关使它不起作用...我得到错误"语法错误:意外的令牌案例和" #34;

var shouldWeapon = String("sword");
var user = prompt("There's a duck in a pond. It likes fish. What do you do? Would you like to feed it, kill it, skin it, buy it, or fight it").toLowerCase();

switch(user) {
    case 'feed it': 
        var whatHaveFood = prompt("What do you have for food?").toLowerCase();
            switch(whatHaveFood) {
                case 'pancakes':
                    console.log("Great! Ducks love their pancakes!");
                    break;
                case 'muffins':
                    console.log("I'm sorry what? You carry muffins? Ducks LOOOOOOOOOOOVE MUFFINS LIKE OMIGOSH I LOVE MUFFINS MMMM M M MMMM MMM IN MY TUMMY.");
                    break;
                case 'dormant spiders':
                    console.log("You decide not to give them to the duck. They're yours. Nobody gets your dormant spiders.");
                    break;
                case 'apple':
                    console.log("OH BOY I LOVE APPLES -said no duck ever.");
                    break;
                default:
                    console.log("The Duck doesn't like that. He curses you to the pits of hell and walks away.");
                    break;
            };
        break;
    case 'kill it':
            var whatHaveWeapon = prompt("What sort of weapon do you have?").toLowerCase();
            if(shouldWeapon || whatHaveWeapon){
                console.log("Why Aren't you using a sword? Why are you using a " + String(whatHaveWeapon) + ". They Suck!");
                }else{
                    console.log("Good choice. The Duck is vanquished.");
                }
        break;
    case 'skin it':
            var tempCat = prompt("What temperature is the cat?");
            if(tempcat > 4){
                console.log("Don't skin ducks.");
            }
            else{
                console.log("That's a freaking cold cat.");
            }
        break;
    case 'buy it':
            var buyDuckCost = Math.floor(Math.random()*5 + 1);
            var buyDucky = ("How much money do you have?");
            var missingMoney = buyDuckCost - buyDucky;
            if(buyDucky >= buyDuckCost){
                console.log("You have bought a duck! congratulations!");
            }
            else{
                console.log("I'm sorry you don't have that much money. You still need" + String(missingMoney) + "$! The duck pulls out a gun and shoots you.");
        break;
    case 'fight it':
        var Smickle = true
        var Donkey = false
        if(Donkey || Smickle){
            console.log("YOU CAN'T FIGHT THE DUCK. THE DUCK IS TOO STRONG");
        }
        else{
            console.log("Ummmm... this is the only accessible answer..... OMEGA GOOD JOB*Cute anime loli voice.*")
        }
        break;
    console.log("What? You're going to do what with the duck?")
    default:

}

据我所知,这应该起作用......

1 个答案:

答案 0 :(得分:2)

在这部分中("买它"案例),你错过了这个结束支撑。

else {
  console.log("I'm sorry you don't have that much money. You still need" + String(missingMoney) + "$! The duck pulls out a gun and shoots you.");
} //<<-- missing this end brace
break;

代码正常工作here