Javascript摇滚,纸,剪刀游戏

时间:2016-05-07 17:12:37

标签: javascript

我一直在用JavaScript编写这个Rochambeau游戏,到目前为止,当我运行它时,它会提示用户输入,但无论你输入什么,结果总是一个平局。这是我的代码:



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);

function compare(userChoice, computerChoice){
    if(userChoice === computerChoice){
        alert("The result is a tie!");
    }
    else if(computerChoice === "scissors"){
        alert("rock wins!");
        if(computerChoice === "paper"){
            alert("paper wins!");
            }
        }
    else if(computerChoice === "rock"){
        alert("rock wins!");
        if(computerChoice === "scissors"){
            alert("scissor wins!");
            }
        }
    else if(computerChoice === "rock"){
        alert("rock wins!");
        if(computerChoice === "paper"){
            alert("scissors wins!");
            }
        }
}
compare();
&#13;
&#13;
&#13;

知道为什么这不能运行剩下的条件吗?

2 个答案:

答案 0 :(得分:0)

您没有将任何参数传递给该函数。函数中的参数默认为undefined。所以undefined === undefined等于true。你应该这样做:

compare(userChoice, computerChoice);

答案 1 :(得分:-1)

  

比较();

你错过了参数吗?