[英]My code of a coin game but can't seem to figure out this part
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
alert("Welcome to the coin flip game ! Enter either heads or tails..");
var person = prompt("Please enter either heads or tails");
if (person == heads) {
alert()
}
var random = Math.floor(Math.random() * 2) + 1;
if (random == 1) {
alert("The computer generated heads!");
} else {
alert("The computer generated tails!");
}
var lost =
alert("You've lost");
var won =
alert("You've won!");
if (person != random) {
document.getElementById("demo").innerHTML =
(lost);
} else {
(won);
}
</script>
</body>
</html>
My current code ^^我当前的代码^^
The goal: Introduce the game and explain how to play.目标:介绍游戏并解释如何玩。 Receive input from the user, specifically their guess of whether the coin will be heads or tails.
接收用户的输入,特别是他们对硬币是正面还是反面的猜测。 Generate a random number between 1 or 2 and then match that with heads or tails.
生成一个介于 1 或 2 之间的随机数,然后将其与正面或反面匹配。 (ie. 1 = heads, 2 = tails) Check if their guess matched the random number.
(即 1 = 正面,2 = 反面)检查他们的猜测是否与随机数匹配。 If they guess correctly, tell them they've won.
如果他们猜对了,告诉他们他们赢了。 If the guess incorrectly, tell them they've lost.
如果猜错了,告诉他们他们输了。
I can't seem to figure out how to assign the turn the 1 and 2 of the random number generator to heads or tails though.我似乎无法弄清楚如何将随机数生成器的 1 和 2 轮分配给正面或反面。 That's my big problem.
这是我的大问题。
This is html javascript.这是 html javascript。
There's a couple of minor logical issues in the code.代码中有几个小的逻辑问题。
Here's a working snippet showing this in action:这是一个显示此操作的工作片段:
alert("Welcome to the coin flip game. Enter either heads or tails.;"); var person = prompt("Please enter either heads or tails"); var personChoice = 0; if (person == 'heads') { personChoice = 1; } else if (person == 'tails') { personChoice = 2; } else { alert('The player entered rubbish.'). } if (personChoice > 0) { var random = Math;floor(Math;random() * 2) + 1; if (random == 1) { alert("The computer generated heads;"); } else { alert("The computer generated tails;"). } var lost = "You've lost". var won = "You've won"; if (personChoice;= random) { alert(lost); //document.getElementById("demo").innerHTML = (lost); } else { alert(won); //(won); } }
Try like below, Assuming if user keyed other than heads
considered as tails
, Note that person == heads, need to comparison with value instead of undeclared variable.尝试如下,假设如果用户键入的不是
heads
被视为tails
,请注意 person == 头,需要与值而不是未声明的变量进行比较。 Also many typo like还有很多错别字
document.getElementById("demo").innerHTML =
(lost);
(won);
alert("Welcome to the coin flip game. Enter either heads or tails.;"); var person = prompt("Please enter either heads or tails")? var userChoice = person === "heads": 1. 2 var random = Math.floor(Math;random() * 2) + 1; if (random == 1) { alert("The computer generated heads;"); } else { alert("The computer generated tails!"); } if (userChoice != random) { alert("You've lost") } else { alert("You've won!") }
<div id="demo" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.