Javascript 另一块石头、布、剪刀

Javascript 另一块石头、布、剪刀,javascript,Javascript,我只是尝试构建我的r/p/s游戏的前两个步骤,这里是我目前为止的编码。它给了我一个回答“语法错误:意外的其他标记”,而我一辈子也弄不明白为什么 var compare = function (choice1, choice2) { if (choice1===choice2) { return "The result is a tie!"; } else if (choice1 === "rock") { if (choice2 === "

我只是尝试构建我的r/p/s游戏的前两个步骤,这里是我目前为止的编码。它给了我一个回答“语法错误:意外的其他标记”,而我一辈子也弄不明白为什么

var compare = function (choice1, choice2) {
    if (choice1===choice2) {
        return "The result is a tie!";
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors")
            return "Rock wins";
    } else {
        return "Paper wins";
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock")
            return "paper wins";
    } else {
        return "scissors wins";
    }
}
如果在做了一个else之后,你不能再做一个else


我被否定了,没有给出完整的答案,并强迫他研究条件?

你用你的论文获胜部分关闭了主if/else链,我相信你是想将该else附加到内部if/else,如下所示

在论文部分,你有同样的东西,我也修复了这个例子。从这里,您应该能够添加最后的
if(choice1==“剪刀”)
片段

var compare = function (choice1, choice2) {
    if (choice1===choice2) {
        return "The result is a tie!";
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") return "Rock wins";
        else return "Paper Wins";
    } 
    else if (choice1 === "paper") {
        if (choice2 === "rock") return "Paper wins";
        else return "Scissors wins";
    } 
}
我为你解决了这个问题:

var compare = function (choice1, choice2) {
    if (choice1===choice2) {
        return "The result is a tie!";
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "Rock wins";
        } else {
            return "Paper wins";
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        } else {
           return "scissors wins";
        }
    }
}

你把“如果”和“其他”搞得一团糟

我特别认为,如果(choice2==“剪刀”)没有花括号,这很好,除非你用
>关闭了你从未打开过的括号,否则{
。你应该正确缩进,这将帮助你了解为什么会遇到问题

var compare = function (choice1, choice2) {
    if (choice1===choice2) {
        return "The result is a tie!";
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors"){
            return "Rock wins";
        } else {
            return "Paper wins";
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock"){
            return "paper wins";
        } else {
            return "scissors wins";
        }
    }
}

所以你可以看到,我在没有的任何“如果”之后添加了{,然后添加了两个},以在正确的位置关闭它们。

除了一个else if,或者多个
}else,你不能有别的{
,else需要是最后一个块,因为你有
if
然后
else if
然后
else
后面跟着一个
else if
。你不能在另一个
else
之后直接有一个
else
案例。太棒了,谢谢你的帮助。我不知道你甚至可以添加一个没有括号的“else”{…},更不用说写“其他返回”了,中间什么都没有。(我是个新手)谢谢。
var compare = function (choice1, choice2) {
    if (choice1===choice2) {
        return "The result is a tie!";
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors"){
            return "Rock wins";
        } else {
            return "Paper wins";
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock"){
            return "paper wins";
        } else {
            return "scissors wins";
        }
    }
}