我只是想知道这个javascript代码有什么问题?

时间:2013-07-07 13:42:36

标签: javascript

以下是代码:

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脚本,在提交代码后,它会出现错误说

  

比较纸张和摇滚时,比较功能不会返回正确的字符串。

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分: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!";
       }
   }
};