?
Solved

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)

Posted on 2005-05-13
2
Medium Priority
?
171 Views
Last Modified: 2012-05-05
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>
0
Comment
Question by:sjsharp80
  • 2
2 Comments
 
LVL 8

Expert Comment

by:NETTY4
ID: 13994470
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
 
LVL 8

Accepted Solution

by:
NETTY4 earned 2000 total points
ID: 13994477
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question