JSFiddle - React, Tailwind, and code Playground
by PiereDome
HTML
<input type='button' id='roll' value='Roll' />
JavaScript
//Daily Programmer #102 [EASY]
function rollDice(string) {
var total = 0,
input = string.split('d');
a = input.length > 1 ? input.splice(0, 1) : 1;
input = input[0].split('-');
if (input.length > 1) {
sign = -1;
} else {
input = input[0].split('+');
sign = 1;
}
b = input[0];
c = input[1];
for (i = 0; i < a; i++) {
total += Math.round(Math.random() * b);
}
total += c * sign;
alert(total);
return total;
}
document.getElementById('roll').addEventListener('click',function(){onclick=rollDice("6+2")},false);