我一直试图用石头剪刀上的javascript制作这个简单的游戏。当我在Microsoft Edge和Chrome上打开它时,不显示alert命令。这是我的代码,我需要帮助解决这个问题。
答案 0 :(得分:0)
当你做var compare = function()....定义一个函数。 所以你有一个调用这个函数来得到一个结果。 尝试做
file
这只是一个例子,假设你每次都会剪刀
答案 1 :(得分:0)
首先你需要调用该函数。所以最好给一个函数名称来调用它,试试这个:
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);
var compare = toCompare(userChoice, computerChoice);
function toCompare(userChoice, computerChoice){
if (userChoice === computerChoice){
alert("The result is a tie!");
}
if(userChoice === "rock"){
if (computerChoice === "scissors"){
alert("rock wins");
}
else{
alert("paper wins");
}
}
if(userChoice === "paper"){
if (computerChoice === "rock"){
alert("paper wins");
}
else{
alert("scissors wins");
}
}
if(userChoice === "scissors"){
if (computerChoice2 === "paper"){
alert("scissors wins");
}
else{
alert("rock wins");
}
}
};