我刚开始学习 javascript,并在 codeacademy 上做了一个练习,制作了一个基于文本的石头剪刀布游戏。我想在调试控制台之外向某人展示它,但我不知道如何在 html 页面中显示它。
我的html文件中的代码是:
<HTML>
<HEAD>
<script src="rockpaperscissors.js"></script>
</HEAD>
<BODY>Choose rock, paper or scissors and write your choice in the text box
<p>
<A HREF="javascript:processOrder();">Click here to begin</A>
</p>
...
</BODY>
</HTML>
我的JS中的代码是:
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";
}
var compare = function (choice1, 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 === "paper") {
if (choice2 === "rock") return "paper wins";
else return "scissors wins";
}
if (choice1 === "scissors") {
if (choice2 === "rock") return "rock wins";
else return "scissors wins";
}
};
compare(userChoice, computerChoice);
我收到选择石头纸或剪刀的提示,但在那之后它没有返回任何东西。我没有在 html 中看到计算机选项,也不知道用什么来显示它。