Reputation: 5
I'm building a small application for my website where-in you are able to input dice rolls that decides if an action was succesful or you hit an enemy (the success ratios are to be added much later) For now though I just need the javascript to show the dice results individually where it right now shows the full number.
Here's the script that outputs the dice roll. I'm starting school in programming on Jan 15th 2018. And I wanna be able to get better. So I appreciate any kind of help you can provide.
<script>
function rollDie(sides) {
if(!sides) sides = 6;
with(Math) return 1 + floor(random() * sides);
}
function rollDice(number, sides) {
var total = 0;
while(number-- > 0) total += rollDie(sides);
return total;
}
</script>
based on own preliminary assumptions it's to do with that final "total" but what to replace it with?
Upvotes: 0
Views: 383
Reputation: 51155
function rollDie(sides) {
if(!sides) sides = 6;
return 1 + Math.floor(Math.random() * sides);
}
function rollDice(number, sides) {
var total = [];
while(number-- > 0) total.push(rollDie(sides));
return total;
}
console.log(rollDice(3, 6))
This is what you are looking for. Instead of summing the dice rolls, just put them in an array and return the array.
Upvotes: 1