From 1ae29437d03ea010da1b87dc12fd155d414e9c1f Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Tue, 22 Aug 2017 16:58:42 -0400 Subject: Implment keepLow --- src/dice.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src') diff --git a/src/dice.js b/src/dice.js index 14addd4..becf7dc 100644 --- a/src/dice.js +++ b/src/dice.js @@ -75,6 +75,25 @@ const keepHigh = (die1, die2) => { } } +const keepLow = (die1, die2) => { + return () => { + const numberToKeep = roll(die1) + let rolls = die2().map(die => [die, die()]) + rolls.sort((a, b) => (a[1] - b[1])) + return rolls.slice(0, numberToKeep).map(pair => { + let returnedOriginal = false + return () => { + if (!returnedOriginal) { + returnedOriginal = true + return pair[1] + } else { + return pair[0]() + } + } + }) + } +} + const bonusAdd = (die1, die2) => { return () => { return die1().map(die => { @@ -92,4 +111,5 @@ exports.subtract = subtract exports.negative = negative exports.explode = explode exports.keepHigh = keepHigh +exports.keepLow = keepLow exports.bonusAdd = bonusAdd -- cgit v1.2.3