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>
sjsharp80Asked:
Who is Participating?
 
NETTY4Commented:
Sorry, wrong index.

Take 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() - 2) ]++;
}


 

            {
             document.write('After 1000 rolls the following results were recorded:' + '<BR>')
             document.write('2 was rolled '  + diceArray[0]  + ' times.' + '<BR>');
             document.write('3 was rolled '  + diceArray[1]  + ' times.' + '<BR>');
             document.write('4 was rolled '  + diceArray[2]  + ' times.' + '<BR>');
             document.write('5 was rolled '  + diceArray[3]  + ' times.' + '<BR>');
             document.write('6 was rolled '  + diceArray[4]  + ' times.' + '<BR>');
             document.write('7 was rolled '  + diceArray[5]  + ' times.' + '<BR>');
             document.write('8 was rolled '  + diceArray[6]  + ' times.' + '<BR>');
             document.write('9 was rolled '  + diceArray[7]  + ' times.' + '<BR>');
             document.write('10 was rolled '  + diceArray[8]  + ' times.' + '<BR>');
             document.write('11 was rolled '  + diceArray[9]  + ' times.' + '<BR>');
             document.write('12 was rolled '  + diceArray[10]  + ' times.' + '<BR>');
             }

}          

0
 
NETTY4Commented:
You need not to change diceRoller() function. Call it only two times.

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>');
             }

}

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.