-6

这是代码:

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

我在 codecademy 中学习 java 脚本,提交代码后,我收到错误消息

将纸张与岩石进行比较时,您的比较函数不会返回正确的字符串。

4

2 回答 2

4

choice1="paper"分配,而不是比较。用于===比较(除非您需要类型强制,在这种情况下使用==

于 2013-07-07T13:44:07.930 回答
0

这应该工作...

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 if(choice2 == "Paper") {
            return "paper Wins!";
       }
   }

   else if (choice1 == "paper") {
       if (choice2 == "Rock") {
            return "paper wins";}
       else if (choice2 == "Scissors") {
            return "scissors wins!";
       }
   }
};
于 2013-07-07T13:48:43.713 回答