Created
March 26, 2018 15:45
-
-
Save 0xCourtney/4d56038428e5e534a71f2b629edaa903 to your computer and use it in GitHub Desktop.
DiceRoll - ToyProblem
This file contains hidden or 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
// Create a function that returns the result of a dice roll | |
// Then modify the function to accept rolling multiple dice of the same size and return the cumulative sum of . | |
// Then modify the function to return an array of objects containing the cumulative sum at the current roll, the dice roll value, and the index. | |
function diceRoll(size = 6, numDice) { | |
let diceSum = 0; | |
let diceArr = []; | |
for (let i = 0; i < numDice; i++) { | |
let roll = Math.floor(Math.random() * (size - 1) + 1); | |
diceSum += roll; | |
let diceObj = { | |
diceValue: roll, | |
currentSum: diceSum, | |
index: i | |
} | |
diceArr.push(diceObj); | |
} | |
return diceArr | |
} | |
diceRoll(20, 15); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment