Last active
July 26, 2019 06:45
-
-
Save nofishleft/c112624d22666dd86d0f446ce64a2542 to your computer and use it in GitHub Desktop.
DND dice roll summary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let input = "1d6 + 2d8 + 5"; | |
//let rollEXP = /[^ ,.+]+/gi; | |
const rollEXP = /(?:[0-9]*d[0-9]+)|(?:[0-9]+)/gi; | |
let segments = input.match(rollEXP); | |
let max = new Array(segments.length); | |
let min = new Array(segments.length); | |
let avg = new Array(segments.length); | |
const segmentEXP = /([0-9]*)d([0-9]+)/i; | |
function parz(i) | |
{ | |
let segment = segments[i]; | |
let parts = segment.match(segmentEXP); | |
if (parts != null) | |
{ | |
let numDice = parseInt(parts[1]) || 1; | |
let numFace = parseInt(parts[2]); | |
max[i] = numFace*numDice; | |
min[i] = numDice; | |
avg[i] = (numDice*(numFace+1))/2; | |
} else | |
{ | |
min[i] = max[i] = avg[i] = parseInt(segment); | |
} | |
} | |
for (let i = 0; i < segments.length; ++i) parz(i); | |
function sum(array) { | |
let t = 0; | |
array.forEach((val) => { | |
t += val; | |
}); | |
return t; | |
} | |
let MAX = sum(max); | |
let MIN = sum(min); | |
let AVG = sum(avg); | |
/* | |
For input = "1d6 + 2d8 + 5" | |
MAX = 27 | |
MIN = 8 | |
AVG = 17.5 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment