坚持www.codeacademy.com语法错误意外令牌

时间:2014-04-17 15:00:20

标签: javascript

我已经在codeacademy网站(Javascript,第4章第8节,石头剪刀)上坚持了这个问题已经有将近3个星期了,我似乎无法弄清楚这意味着什么。

错误消息是SyntaxError:意外的令牌:

var userChoice = prompt("Do you choose rock, paper or scissors?");
var gameTimes = 0;
var computer = functions(); {
    gameTimes = gameTimes + 1;
    var computerChoice = Math.random();
    if (computerChoice <= 0.34) {
        computerChoice = "rock";
    } else if (computerChoice <= 0.67) {
        computerChoice = "paper";
    } else {
        computerChoice = "scissors";
    }
    console.log(gameTimes + ".Computer:" + computerChoice);
    return computerChoice;
}
var compare = function (choice1, choice2) {
    if (choice1 === choice2) {
        console.log("Your choice: " + userChoice);
        console.log("The result is a tie!");
        userChoice = prompt("Please make the choice again!");
        compare(userChoice, computerChoice());
    } else if (choice1 === "rock") {
        return "paper wins";
    } else {
        return "scissors wins";
    }
    console.log("Your choice: " + userChoice);
    if (choice2 === "scissors")
        console.log("You win!");
    else
        console.log("Computer wins!");
};
else if (choice1 === "paper") {
    console.log("Your choice: " + userChoice);
    if (choice2 === "rock")
        console.log("You win!");
    else
        console.log("Computer wins!");
} else {
    console.log("Your choice: " + userChoice);
    console.log("The choice is invalid!");
    userChoice = prompt("Please enter valid choice again!");
    compare(userChoice, computerChoice());
}

4 个答案:

答案 0 :(得分:1)

你的语法不对。看看

var computer = functions();{ 

应该是

var computer = functions() {

并且在

之前底部没有if语句
else if(choice1 === "paper")
{
    console.log("Your choice: "+ userChoice);
    if(choice2 === "rock")
    console.log("You win!");
    else
    console.log("Computer wins!");
}
else
{
    console.log("Your choice: "+userChoice);
    console.log("The choice is invalid!");
    userChoice=prompt("Please enter valid choice again!");
    compare(userChoice,computerChoice());
}

答案 1 :(得分:1)

有多个错位的分号:

第3行:

var computer = functions();{

接近尾声(可能是给出意外的其他令牌错误的那个):

};
    else if

答案 2 :(得分:0)

第3行中的

var computer = functions();{应为var computer = function(){

答案 3 :(得分:0)

那是因为您的else if之前没有if

只有在else声明的前提下,您才可以使用else ifif

另外,你有一些语法错误,你应该纠正。