我需要有人为我查看一些javascript代码,因为我收到一个错误,指出语法错误

时间:2015-03-22 23:18:25

标签: javascript switch-statement

我需要有人来查看这段代码。我是javascript的初学者,我在这段代码中找不到错误。

 var user = prompt ("Are you a goblin, knight, troll, human, or wizard?").toLowerCase()
var name = prompt("What is your name?").toLowerCase()
var gender = prompt("Are you male or female?").toLowerCase()
switch(user) {
        case 'goblin':
            console.log("Hello, you are going to have a fun journey as a goblin");
            var meetAnt = prompt ("You run into a giant ant eater, you can attack or run. Do you choose to attack?").toLowerCase()
            if(meetAnt = yes) {
                console.log ("you stab him in the eye, but then he hits you with his trunk. You are injured");
                var finishAnt = prompt("you can either risk your life and finish him, or try to run away. Do you finish him?").toLowerCase()
                if(finishAnt = yes) {
                    return true;
                    console.log("congratulations you have killed the aint eater!")
                }
                else if(finishAnt = no) {
                    return false;
                    console.log("You run away and barley make it, you are badly hurt");
                    else {
                        console.log("sorry thats not an option")
                    }
                }
            }
            else {
                return true;
                console.log("You run past the ant eater and get to saftey. You are very lucky")
            }
            break;
        case 'knight':
            console.log("Hello, you are going to have a fun journey as a knight");
            var dayFight = prompt("You are versing the goblins in a fight, you need to choose how many days you go to fight(1-100). Remember if you choose too little days then you won't have stayed long enough to win the fight but if you stay to long you have more of a chance of dying or getting injured").toLowerCase()
            if(dayFight < 40) {
                return false;
                console.log("You did not fight enough days, which has resulted in your kingdome loosing the war to the goblins.");
            }
            else if(dayFight > 60) {
                return false;
                console.log("You have went to war to long and been seriously injured.");
            }
            else if(40<=dayFight=>60) {
                return true;
                console.log("You have been at war for the right amount of time. You have came out of the war with no injuries and you also have defieted the goblins!")
            }
            break;
        case 'troll':
            console.log("Hello, you are going to have a fun journey as a troll");
            var red = true;
            var green = true;
            var yellow = false;
            var blue = false;
            var houseRaid = prompt("You see four huts in the middle of the woods. You can raid two. There is a red hut, blue hut, yellow hut, and green hut. What is one color house that you want to raid?").toLowerCase()
            var doorPick = prompt("Do you want to enter throuhg the back door or front door?").toLowerCase()
            if(doorPick||houseRaid = "red"||"green" && "back door") {
                return true;
                console.log("You raided these houses and left before any villagers would see you");
            }
            else {
                return false;
                console.log("You raided those houses, but one of them was a booby trap and you are now captured");
            }
            break;
        case 'human':
            console.log("Hello, you are going to have a fun journey as a human");
            var reinforceFound = prompt("You know a storm is comming and you have to reinforce your hut, but you only have enough material to reinforce either your lower foundations or higher foundations. Which do you inforce? Higher or lower?").toLowerCase()
            if(reinforceFound = lower) {
                return false;
                console.log("The storms winds pushed down the top of your house and caved in your roof.");
            }
            else if(reinforceFound = higher) {
                return true;
                console.log("The storm did not do that much damage to your house due to your reinforced higher foundations. Good choice");
            }
            else {
                console.log("sorry but that is not an option. Pick either 'higher', or 'lower'")
            }
            break;
        case 'wizard':
            console.log("Hello, you are going to have a fun journey as a wizard");
            var blood = true;
            var dust = true;
            var wings = false;
            var mushrooms = false;
            var postion = prompt("You are working on a new healing potion but you do not know what you need to add to finish it. You have 4 ingrediants; blood, dust, wings, mushrooms. Pick one too add. (WARNING: Pick carefully because if you choose the wrong ingerdiant, then your potion will be ruined.)").toLowerCase()
            if(postion = wings || mushroom) {
                console.log("You picked a bad ingrediant and now your potion is ruined.");
            }
            else if(postion = dust || blood) {
                console.log("you picked the right ingrediant and your potion is okay")
            }
            else {
                console.log("sorry but that is not an option");
            }
            break;
        default:
            console.log("Sorry but that is not a character in the game");
};

我正在为www.codecademy.com网站上的课程编写此代码。它应该是游戏的一小部分。对不起有这么多,我不能再缩小错误来自。

1 个答案:

答案 0 :(得分:1)

语法错误是因为您在第18行(已发布代码)中缺少结束括号。这样:

else {

应该是:

} else {

缺少括号表示if之前没有匹配的else

代码中的其他一些问题,但可能不是全部问题:


在许多陈述结束时,您缺少分号。当语句在行结束时结束时不需要它们,但建议使用它们。


您遇到以下比较问题:

if(meetAnt = yes) {

比较运算符是==(或===),并且您缺少字符串值周围的分隔符,因此它将被解释为变量名称。它应该是:

if(meetAnt == "yes") {

在这些比较中,您会得到意想不到的结果:

if(dayFight < 40) {

变量包含一个字符串,因此值40将转换为字符串"40"以进行比较,并将它们作为字符串而不是数字进行比较。这意味着例如"100" < "40"。您应该将输入字符串解析为数字:

dayFight = parseInt(dayFight, 10);

通过这样的比较,语法是错误的:

else if(40<=dayFight=>60) {

使用两个运算符比较一个值不起作用,并且没有=>运算符。你需要两个条件:

else if(40 <= dayFight && dayFight <= 60) {

在这样的比较中,您使用了||运算符错误(以及错误的比较运算符和缺少的字符串分隔符):

if(postion = wings || mushroom) {

它在条件之间使用,而不是在一个条件下比较多个值:

if(postion == "wings" || postion == "mushroom") {

当您在return之后放置语句时,该语句将不会执行,因为代码将退出return语句中的函数:

return true;
console.log("congratulations you have killed the aint eater!")

重新排序:

console.log("congratulations you have killed the aint eater!")
return true;