Skip to content

Instantly share code, notes, and snippets.

@shaefer
Created June 5, 2018 21:09
Show Gist options
  • Save shaefer/149be3c9c288ca24e18236c16fea1090 to your computer and use it in GitHub Desktop.
Save shaefer/149be3c9c288ca24e18236c16fea1090 to your computer and use it in GitHub Desktop.
DiceBag Phase 2
import {sum} from './ArrayUtils'
export const d6 = () => {
return rollDice(1, 6);
}
export const rollDice = (numOfDice, numOfSides) => {
if (numOfDice > 100000) throw new Exception("Max dice of 10000 exceeded");
if (numOfSides > 100000) throw new Exception("Max sides of 10000 exceeded");
let individualResults = [];
for(let i = 1;i <= numOfDice;i++) {
const result = Math.floor(Math.random() * numOfSides) + 1;
individualResults.push(result);
}
return {
total: sum(individualResults),
individualResults: individualResults
};
}
const DiceBag = {
d6: d6,
rollDice: rollDice
};
export default DiceBag;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment