Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let diceNumber = 0;
- let dices = null;
- let conditionFunction = null;
- let limit = 0;
- let intervals = [];
- let res = 0;
- let counter = 0;
- function resetState() {
- document.getElementById("results").innerHTML = "";
- document.getElementById("dices").innerHTML = "";
- diceNumber = document.getElementsByName("diceNumber")[0].value;
- for (let i = 0; i < diceNumber; i++) {
- document.getElementById("dices").innerHTML += "<span class=\"dice\">⚀</span>";
- }
- dices = document.getElementsByClassName("dice");
- limit = document.getElementsByName("limit")[0].value;
- let condition = document.querySelector('input[name="condition"]:checked').value;
- if (condition === "more") {
- conditionFunction = function(res, limit) { return res > limit; }
- } else if (condition === "less") {
- conditionFunction = function(res, limit) { return res < limit; }
- } else {
- conditionFunction = function(res, limit) { return res == limit; }
- }
- counter = 0;
- }
- function submitRolls() {
- resetState();
- roll();
- let interval = setInterval(function() {
- if(conditionFunction(res, limit)) {
- clearInterval(interval);
- } else {
- roll();
- }
- counter++;
- let text = "<span>Bacanje broj " + counter + ": " + res +"<br>";
- for (let i = 0; i < diceNumber-1; i++) {
- text += dices[i].getAttribute("value") + " + ";
- }
- text += dices[diceNumber-1].getAttribute("value") + "</span><br>";
- document.getElementById("results").innerHTML += text;
- }, diceNumber * 400 + 100);
- }
- function roll() {
- let current = 0;
- for (let i = 0; i < diceNumber; i++) {
- intervals[i] = setInterval(function() { rolldice(i); }, 50);
- setTimeout(function() {
- clearInterval(intervals[i]);
- current += parseInt(dices[i].getAttribute("value"));
- if (i == diceNumber-1) {
- res = current;
- }
- }, (i+1) * 400);
- }
- }
- function rolldice(x) {
- let ranNum = Math.floor( 1 + Math.random() * 6 );
- dices[x].innerHTML = "&#" + (9855 + ranNum) + ";";
- dices[x].setAttribute("value", ranNum);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement