我只是想知道这个java脚本代码中出了什么问题

I just want to know whats wrong in this java script code?

本文关键字:代码 问题 什么 脚本 java 想知道      更新时间:2023-09-26

以下是代码:

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脚本,在提交代码后,它会给我一个错误,说

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

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 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!";
       }
   }
};