Advertisement
marichan022

rwa 5. zadatak

Nov 14th, 2019
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let diceNumber = 0;
  2. let dices = null;
  3. let conditionFunction = null;
  4. let limit = 0;
  5. let intervals = [];
  6. let res = 0;
  7. let counter = 0;
  8.  
  9. function resetState() {
  10.     document.getElementById("results").innerHTML = "";
  11.     document.getElementById("dices").innerHTML = "";
  12.  
  13.     diceNumber = document.getElementsByName("diceNumber")[0].value;
  14.     for (let i = 0; i < diceNumber; i++) {
  15.         document.getElementById("dices").innerHTML += "<span class=\"dice\">&#9856;</span>";
  16.     }
  17.     dices = document.getElementsByClassName("dice");
  18.  
  19.     limit = document.getElementsByName("limit")[0].value;
  20.  
  21.     let condition = document.querySelector('input[name="condition"]:checked').value;
  22.     if (condition === "more") {
  23.         conditionFunction = function(res, limit) { return res > limit; }
  24.     } else if (condition === "less") {
  25.         conditionFunction = function(res, limit) { return res < limit; }
  26.     } else {
  27.         conditionFunction = function(res, limit) { return res == limit; }
  28.     }
  29.  
  30.     counter = 0;
  31. }
  32.  
  33. function submitRolls() {
  34.     resetState();
  35.  
  36.     roll();
  37.     let interval = setInterval(function() {
  38.         if(conditionFunction(res, limit)) {
  39.             clearInterval(interval);
  40.         } else {
  41.             roll();
  42.         }
  43.  
  44.         counter++;
  45.         let text = "<span>Bacanje broj " + counter + ": " + res +"<br>";
  46.         for (let i = 0; i < diceNumber-1; i++) {
  47.             text += dices[i].getAttribute("value") + " + ";
  48.         }
  49.         text += dices[diceNumber-1].getAttribute("value") + "</span><br>";
  50.         document.getElementById("results").innerHTML += text;
  51.  
  52.     }, diceNumber * 400 + 100);
  53. }
  54.  
  55. function roll() {
  56.     let current = 0;
  57.     for (let i = 0; i < diceNumber; i++) {
  58.         intervals[i] = setInterval(function() { rolldice(i); }, 50);
  59.         setTimeout(function() {
  60.             clearInterval(intervals[i]);
  61.             current += parseInt(dices[i].getAttribute("value"));
  62.             if (i == diceNumber-1) {
  63.                 res = current;
  64.             }
  65.         }, (i+1) * 400);
  66.     }
  67. }
  68.  
  69. function rolldice(x) {
  70.     let ranNum = Math.floor( 1 + Math.random() * 6 );
  71.     dices[x].innerHTML = "&#" + (9855 + ranNum) + ";";
  72.     dices[x].setAttribute("value", ranNum);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement