JSFiddle - React, Tailwind, and code Playground

by shryme

HTML

<p id="text">1</p>
<input type="button" value="roll" onclick="rollDice()">

JavaScript

var diceRolls = [];
var getallen = [1, 2, 3, 4, 5, 6];
var totalDiceRolls = 0;
var maxDiceRolls = 40;
var totalPoints = 40;

diceRolls.push(5);
diceRolls.push(5);
diceRolls.push(5);
diceRolls.push(4);
diceRolls.push(3);
diceRolls.push(3);
diceRolls.push(1);
diceRolls.push(2);
diceRolls.push(2);


function rollDice() {

    if (++totalDiceRolls > maxDiceRolls) {
        alert(maxDiceRolls + " dice rolls allowed at max!");
        return;
    }

    totalPoints--;

    var d1 = getallen[Math.floor(Math.random() * getallen.length)];
    d1 = 5;
    console.log("You roll the dice, the number is " + d1 + ".");
    diceRolls.push(d1);
    console.log(formatedDice(diceRolls));



    if (diceRolls[diceRolls.length - 2] === d1) {
        totalPoints += 5;
        console.log("You won 5 points!!");
    }
    console.log("You now have " + totalPoints + " points left.");
}

function formatedDice(dr) {
    var str = "";
    var isDuplicated = false;
    for (var i = 0; i < dr.length; i++) {
        if (!isDuplicated && dr[i+1] !== null && dr[i] === dr[i+1])
        {
            isDuplicated = true;
            str += "(" + dr[i];
        }
        else if (isDuplicated && dr[i+1] !== null && dr[i] !== dr[i+1])
        {
            isDuplicated = false;
            str += dr[i] + ")";
        }
        else
        {
             str += dr[i];   
        }
    }

    return str;
}