Selecting the number of dice in a movie in JavaScript Dice Roller

I am trying to get my script to allow the user to select how many bones they would like to collapse (1-40) in my input field using the onClick () button, which captures this input number and the display / creation of the "cubes" div.

    <script>
    // ORIGINAL DICE ROLLER CODE, KEEP JUST IN CASE!!!//
      function rollDice(){
        var numDice = document.getElementById("diceNum").value;
        var status = document.getElementById("status");
        var d1 = Math.floor(Math.random() * 6) + 1;
        var diceRoll = d1;

        var container = document.getElementById("diceNum").value;

        for (var i = 0; i < diceNum.value; i++){
          //LOOP THROUGH APPHENDED INPUT VALUE AND POPULATE CONTAINER DIV//
          container.innerHTML = '<div class="dice">+diceRoll+</div>';
        };
        numDice.innerHTML = diceRoll;
        status.innerHTML = "You rolled "+diceRoll+".";+
      };


    // DICE ROLLER V(1.1 MOD) //
      function showdice() {
        var dicenum = prompt("how many dice would you like to roll?");
        var diceint = Math.round(dicenum);
        var diceroll, results = '';

        if (diceint >= "50") {
          alert("we don't have that many dice");
        }

        else if (diceint <= "0") {
          alert("you need to roll at least one die");
        }

        else
          for (i = 1; i <= diceint; i++) {
            diceroll = Math.ceil(Math.random() * 6);
            results += diceroll + ',';
          }
          alert(results);
      }


    // DICE ROLLER V(1.2 MOD) //
      function showdice() {
        var dicenum = prompt("how many dice would you like to roll?");
        var diceint = Math.round(dicenum);
        var results = [];

        if (diceint >= "50") {
          alert("we don't have that many dice");
        }

        else if (diceint <= "0") {
          alert("you need to roll at least one die");
        }

        else
          for (i = 0; i < diceint; i++) {
            results[i] = Math.ceil(Math.random() * 6);
          }
          alert(results.join(', '));
        }
    </script>
  </head>

  <body>
    <br/><br/><br/>

      <form class="" action="index.html" method="post">
        <h4>Choose number of dice to roll</h4>
        <p>[1 - 40]</p>
        <br/>
        <input id="diceNum" type="text" name="DiceNumber" placeholder="">
        <br/>
      </form>

      <br/><br/>

      <div id="dieContainer" class="dice">0</div>
      <br/><br/>
      <button onclick="rollDice()" style="margin-left: 20px; margin-right: 20px;">Roll Dice</button>
      <h2 id="status" style="clear:left;"></h2>
  </body>

I added two different solutions, listed as V (1.1) and V (1.2), since I only want to accomplish this using strictly JavaScript and HTML.

[EDIT] Both versions have been tested, although I seem to be unable to get the correct logic so that either one works correctly.

How can I go on to capture an input value and create / display multiple div cubes?

+4
1

:

function rollDice() {
  var numDice = document.getElementById("diceNum").value;
  var container = document.getElementById("dieContainer");

  container.innerHTML = "";

  for (var i = 0; i < numDice; i++) {
    var diceRoll = Math.floor(Math.random() * 6) + 1;
    container.innerHTML += '<div class="dice">' + diceRoll + '</div>';
  };
};
<h4>Choose number of dice to roll</h4>
<input id="diceNum" type="text" value="3">
<button onclick="rollDice()">Roll Dice</button>
<div id="dieContainer" class="dice"></div>
Hide result
+2

Source: https://habr.com/ru/post/1679173/


All Articles