简体   繁体   English

我的硬币游戏代码,但似乎无法弄清楚这部分

[英]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.代码中有几个小的逻辑问题。

  1. When the player enters either heads or tails, this can be translated to 1 or 2 in the code for easier comparison with the randomly generated value.当玩家输入正面或反面时,可以在代码中将其转换为 1 或 2,以便与随机生成的值进行比较。
  2. If the player enters a junk value this should be handled.如果玩家输入垃圾值,则应进行处理。
  3. I'm not sure I understood what the code to display the result was doing so have just changed it to show an alert with won or lost.我不确定我是否理解显示结果的代码是做什么的,所以只是将其更改为显示获胜或失败的警报。

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.

相关问题 我的CSS问题似乎无法弄清楚 - Issue with my CSS that I can't seem to figure out 似乎无法弄清楚为什么我的JavaScript无法在IE和Chrome中运行 - Can't seem to figure out why my javascript isn't working in IE and Chrome 我的javascript函数似乎无法正常工作。 我不知道为什么 - My javascript function doesn't seem to be working. I can't figure out why 似乎无法弄清楚为什么我的 promise 阵列不工作 - Can't seem to figure out why my promise array isn't working 我似乎无法弄清楚如何进行字符串插值; 代码排练习 - I can't seem to figure out how to do a string interpolation; Code Platoon Exercise 无法弄清楚如何将引导 js 插入我的代码 - Can't figure out how to insert bootstrap js into my code 无法弄清楚我的JavaScript代码出了什么问题 - Can't figure out what is wrong with my JavaScript code 我的CPU在Google Cloud上的使用率已达到160%,我似乎不知道为什么吗? - My CPU is reaching 160% on Google Cloud and I can't seem to figure out why? d3:我的路径似乎在翻倍,我不知道为什么 - d3: My paths seem to be doubling up and I can't figure out why 我的json请求不会触发beforeSend请求。似乎无法找出原因 - My json request will not fire a beforeSend request.. Can't seem to figure out why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM