我有一个 javascript 中的石头、纸、剪刀模拟器(在一个文件中),它接受用户输入,生成一个 AI 选择,然后比较它们,然后返回答案。我将如何在 HTML 字段中完成所有这些操作,以便用户可以看到正在发生的一切?代码如下:
var userChoice = prompt("Do you choose rock, paper or scissors?");
console.log("You chose: " + userChoice);
var compare = function (choice1) {
var choice2 = Math.random();
if (choice2 < 0.34) {
choice2 = "rock";
console.log("The computer chose: " + choice2);
} else if(choice2 <= 0.67) {
choice2 = "paper";
console.log("The computer chose: " + choice2);
} else {
choice2 = "scissors";
console.log("The computer chose: " + choice2);
}
if (choice1 === choice2) {
return "The result is a tie!";
}
if (choice1 === "rock") {
if (choice2 === "scissors") {
return "rock wins";
} else {
return "paper wins";
}
}
if (choice1 === "scissors") {
if (choice2 === "rock") {
return "rock wins";
} else {
return "scissors win";
}
}
if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
return "scissors win";
}
}
};
compare(userChoice);