sjsharp80
asked on
Modify an existing script to take account of rolling 2 dice as apposed to one to output how many times a result was rolled (2 - 12)
I have the following code that prints out the number of times each value of a dice was rolled for 500 rolls, I now need to modify it so it takes account of 2 dice, I don't want to modify the diceRoller function that performs the dice roll, ideally I would like to load an array with the results of rolling 2 dice (2 - 12) and print out the results of how many times e.g.
2 was rolled 98
3 was rolled 121 etc...
Current script..
<script>
function rollResult () {
var diceArray = [0,0,0,0,0,0];
for( var intIndex = 0; intIndex < 500; intIndex++ )
{
diceArray[ (diceRoller() - 1) ]++;
}
{
document.write('After 1000 rolls the following results were recorded:' + '<BR>')
document.write('1 was rolled ' + diceThrowArray[0] + ' times.' + '<BR>');
document.write('2 was rolled ' + diceArray[1] + ' times.' + '<BR>');
document.write('3 was rolled ' + diceArray[2] + ' times.' + '<BR>');
document.write('4 was rolled ' + diceArray[3] + ' times.' + '<BR>');
document.write('5 was rolled ' + diceArray[4] + ' times.' + '<BR>');
document.write('6 was rolled ' + diceArray[5] + ' times.' + '<BR>');
}
}
</script>
2 was rolled 98
3 was rolled 121 etc...
Current script..
<script>
function rollResult () {
var diceArray = [0,0,0,0,0,0];
for( var intIndex = 0; intIndex < 500; intIndex++ )
{
diceArray[ (diceRoller() - 1) ]++;
}
{
document.write('After 1000 rolls the following results were recorded:' + '<BR>')
document.write('1 was rolled ' + diceThrowArray[0] + ' times.' + '<BR>');
document.write('2 was rolled ' + diceArray[1] + ' times.' + '<BR>');
document.write('3 was rolled ' + diceArray[2] + ' times.' + '<BR>');
document.write('4 was rolled ' + diceArray[3] + ' times.' + '<BR>');
document.write('5 was rolled ' + diceArray[4] + ' times.' + '<BR>');
document.write('6 was rolled ' + diceArray[5] + ' times.' + '<BR>');
}
}
</script>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Like this:
function rollResult () {
var diceArray = [0,0,0,0,0,0,0,0,0,0,0,0];
for( var intIndex = 0; intIndex < 500; intIndex++ )
{
diceArray[ (diceRoller() + diceRoller() - 1) ]++;
}
{
document.write('After 1000 rolls the following results were recorded:' + '<BR>')
document.write('1 was rolled ' + diceThrowArray[0] + ' times.' + '<BR>');
document.write('2 was rolled ' + diceArray[1] + ' times.' + '<BR>');
document.write('3 was rolled ' + diceArray[2] + ' times.' + '<BR>');
document.write('4 was rolled ' + diceArray[3] + ' times.' + '<BR>');
document.write('5 was rolled ' + diceArray[4] + ' times.' + '<BR>');
document.write('6 was rolled ' + diceArray[5] + ' times.' + '<BR>');
document.write('7 was rolled ' + diceArray[6] + ' times.' + '<BR>');
document.write('8 was rolled ' + diceArray[7] + ' times.' + '<BR>');
document.write('9 was rolled ' + diceArray[8] + ' times.' + '<BR>');
document.write('10 was rolled ' + diceArray[9] + ' times.' + '<BR>');
document.write('11 was rolled ' + diceArray[10] + ' times.' + '<BR>');
document.write('12 was rolled ' + diceArray[11] + ' times.' + '<BR>');
}
}