In: Computer Science
I want the program to use 1- D array and display a table as output.
Write a script to simulate the rolling of two dice. The script should use Math.random to roll the first die and again to roll the second die. The sum of the two values should then be calculated. [Note: Since each die can show an integer value from 1 to 6, the sum of the values will vary from 2 to 12, with 7 being the most frequent sum, and 2 and 12 the least frequent sums. Figure 10.22 shows the 36 possible combinations of the two dice. Your program should roll the dice 36,000 times. Use a one-dimensional array to tally the number of times each possible sum appears. Display the results in an HTML5 table. Also determine whether the totals are reasonable (e.g., there are six ways to roll a 7, so approximately 1/6 of all the rolls should be 7).]
<!DOCTYPE html>
<html>
<body>
<script language="javascript" type="text/javascript">
var myArray = new Array();
var min=1;
var max=7;
var i=0;
for(i=1;i<=3600;i++)
{
var random1=Math.floor(Math.random() * (+max - +min)) + +min;
var random2=Math.floor(Math.random() * (+max - +min)) + +min;
myArray[i]=random1+random2;
}
var myTable= "<table><tr><td style='width: 100px;
color: red;'>Col Head 1</td>";
myTable+= "<td style='width: 100px; color: red; text-align:
right;'>Col Head 2</td></tr>";
myTable+="<tr><td style='width: 100px;
'>---------------</td>";
myTable+="<td style='width: 100px; text-align:
right;'>---------------</td></tr>";
for (var i=1; i<=3600; i++) {
myTable+="<tr><td style='width: 100px;'>Sum " + i + "
is:</td>";
myArray[i] = myArray[i];
myTable+="<td style='width: 100px; text-align: right;'>" +
myArray[i] + "</td></tr>";
}
myTable+="</table>";
document.write( myTable);
</script>
</body>
</html>