警报命令对我的javascript代码无效
我一直在尝试在石头剪刀上用javascript制作这个简单的游戏。当我在Microsoft edge和Chrome上打开警报命令时,它不会显示。这是我的密码,我需要帮助警报命令对我的javascript代码无效,javascript,jquery,html,Javascript,Jquery,Html,我一直在尝试在石头剪刀上用javascript制作这个简单的游戏。当我在Microsoft edge和Chrome上打开警报命令时,它不会显示。这是我的密码,我需要帮助 当您执行var compare=function。。。。正在定义一个函数。 所以你需要调用这个函数来得到一个结果。 试着做 这只是一个例子,假设您每次都玩剪刀,首先需要调用函数。因此,最好为调用它指定一个函数名,请尝试以下操作: var userChoice = prompt("Do you choose rock, paper
当您执行var compare=function。。。。正在定义一个函数。 所以你需要调用这个函数来得到一个结果。 试着做
这只是一个例子,假设您每次都玩剪刀,首先需要调用函数。因此,最好为调用它指定一个函数名,请尝试以下操作:
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");
}
}
};
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");
}
}
};