From 3915ca0932333f3d0d01538df915ac38810949cc Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Tue, 11 Jul 2017 21:04:59 -0400 Subject: Fix bug with compound dice --- __tests__/dice.test.js | 8 +++++++- src/dice.js | 12 ++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/__tests__/dice.test.js b/__tests__/dice.test.js index fa2d2dc..4828ce4 100644 --- a/__tests__/dice.test.js +++ b/__tests__/dice.test.js @@ -169,7 +169,7 @@ const combinedDiceTestSpecs = (dieSpecs) => { const expectExtrema = dieSpecs.map(spec => (Math.pow(spec.sides, spec.number))) - .reduce(times) < 50 + .reduce(times) < 50 return { diceCount: combineSpecField(spec => (spec.diceCount)), average: { @@ -305,4 +305,10 @@ describe('compound dice', () => { testDie(die, testSpec) }) + + describe('(2d1)d(2d1)', () => { + const die = d(d(constant(2), constant(1)), d(constant(2), constant(1))) + + testDie(die, basicDieTestSpecs(2, 2)) + }) }) diff --git a/src/dice.js b/src/dice.js index ead3d16..c8a2572 100644 --- a/src/dice.js +++ b/src/dice.js @@ -1,11 +1,15 @@ const constant = n => () => [n] +const roll = (die) => { + return die().reduce((a, b) => (a + b)) +} + const d = (number, sides) => { return () => { let pool = [] - const currentNumber = number() - const currentSides = sides() + const currentNumber = roll(number) + const currentSides = roll(sides) for (let i = 0; i < currentNumber; i++) { pool.push(1 + Math.floor(Math.random() * currentSides)) @@ -33,10 +37,6 @@ const subtract = (die1, die2) => { } } -const roll = (die) => { - return die().reduce((a, b) => (a + b)) -} - exports.constant = constant exports.d = d exports.add = add -- cgit v1.2.3