意外的令牌其他codecademy

时间:2015-08-08 04:34:10

标签: javascript

我遇到了一个问题,我遇到了语法错误(意外的其他) 以下是我的代码。我认为这将是一个"花括号"或分叉问题

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if (computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}
console.log("Computer: " + computerChoice);

var compare = function(choice1, choice2) {
        if (choice1 === choice2) {
            return "The result is a tie!";

        } else if (choice1 === "rock") {
            if (choice2 === "scissors") {
                return "rock swins";
            } else {
                return "paper wins";
            }
        } else if (choice1 === "paper") {
            if (choice2 === "rock") {
                return "paper wins";
            } else {
                return "scissors wins";
            } else if (choice1 === "scissors") {
                if (choice2 === "rock") {
                    return "rock wins";
                } else {
                    return "scissors wins"
                }
            }
        };

任何帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:1)

请放置下面的代码,我为最后一个部分else if (choice1 === "paper")添加了一个缺少的右括号。

var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";

    } else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock swins";
        } else {
            return "paper wins";
        }
    } else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        } else {
            return "scissors wins";
        } else if (choice1 === "scissors") {
            if (choice2 === "rock") {
                return "rock wins";
            } else {
                return "scissors wins"
            }
        }
    } 
};

如果有任何错误,请告诉我。