简体   繁体   English

JavaScript 函数抛硬币函数挑战

[英]JavaScript Functions Toss Coin Function Challenge

JavaScript Functions (need help with question 4) JavaScript 函数(需要帮助解决问题 4)

  1. Define a function that simulates tossing a coin.定义一个模拟抛硬币的函数。 Write your definition here.在这里写下你的定义。 You can save it in your folder as a text file called tossCoin1.txt您可以将其保存为名为 tossCoin1.txt 的文本文件在您的文件夹中

  2. Incorporate your function definition from number one into a program that simulates coin tossing.将第一个函数定义合并到一个模拟抛硬币的程序中。 Call the file tossCoin2.html.调用文件 tossCoin2.html。 Let the program toss the coin each time the user presses the “Toss” button.每次用户按下“Toss”按钮时,让程序抛硬币。 This means that you will need to have a form in the body of your document.这意味着您需要在文档正文中包含一个表单。 Display the results of the toss in a text input in the form.在表单中输入文本显示折腾结果。

  3. Modify the program you just wrote, saving it as tossCoin3.html so that it keeps track of the number of times the coin has been tossed, the number of times heads comes up and the number of times tails comes up.修改您刚刚编写的程序,将其保存为 tossCoin3.html,以便跟踪硬币被抛的次数、正面出现的次数和反面出现的次数。

  4. Modify the program again, saving it as tossCoin4.html so that the user can input the number of times they want to toss the coin.再次修改程序,将其保存为tossCoin4.html,以便用户输入他们想要抛硬币的次数。 If they input a 1000, it will toss the coin a thousand times and output the number of times heads comes up and the number of times tails comes up.如果他们输入 1000,它将掷硬币一千次,并输出正面出现的次数和反面出现的次数。

 <label>How many times do you wat to flip the coin</label> <input type = "number" id = "tosses" required> <input type="submit" onsubmit="flipCoin()" value="Flip Coin"> <p> Results:</p> <p> Heads: <span id="head">0</span></p> <p> Tails: <span id="tail">0</span></p>
  <script>
    var tails = 0;
    var heads = 0;
    var tosses = parseInt(document.getElementById("tosses").value);

    function flipCoin(){
      while (tosses != 0){

        var toss = Math.floor(Math.random() * 2);
        tosses--;

        if(toss == 0){
          heads++;
        } else {
          tails++;
        }
        
        document.getElementById("head").innerHTML = (heads);
        document.getElementById("tail").innerHTML = (tails);
      }
      
    }//flipCoin()
  </script>

This function (after a charitable edit by another user) solves the problem...此功能(在其他用户进行慈善编辑后)解决了问题...

 function flipCoin() { let tails = 0; let heads = 0; let tosses = parseInt(document.getElementById('tosses').value); //Loop as many times as the tosses for (var i = 0; i < tosses; i++) { let toss = Math.random(); if (toss < 0.5) { heads++; } else { tails++; } } //for loop //display results document.getElementById("head").innerHTML = (heads); document.getElementById("tail").innerHTML = (tails); } document.getElementById('run').addEventListener("click", flipCoin);
 Tosses: <input id="tosses" value="100" style="width:100px" /> <button id="run">Run</button> <p>Heads: <span id="head"/></p> <p>Tails: <span id="tail"/></p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM