Created
June 5, 2018 21:09
-
-
Save shaefer/149be3c9c288ca24e18236c16fea1090 to your computer and use it in GitHub Desktop.
DiceBag Phase 2
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
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