1 Replies - 19366 Views - Last Post: 25 June 2005 - 10:53 AM

#1 Trout69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-June 05

Javascript - Simulate Roll of dice

Post icon  Posted 24 June 2005 - 11:22 AM

i have been set what seems a simple task to create a program to roll a die 1000 times and count the number of occurences each number on the die is rolled, and store them in an array for output using document.write.

I;m fairly new to all this and somewhat baffled and would appreciate any input from you experts out there? my code is as below in full....


	<script 
  language="Javascript" 
  type="text/javascript">
  
  var total;
  var totalArray = new Array (6);
  var totalArray = [0, 0, 0, 0, 0, 0];

  var total = (rollDie()-1);
  
  totalArray[total] = totalArray[total] + 1;
  
  
  }

for (var count = 1; count <= 1000; count = count + 1)
	{
  //
  // THIS FUNCTION SIMULATES THE ROLL OF A SINGLE 6-SIDED DIE

  function rollDie () {
  
 	 var total = 0;
 	 
 	 total = Math.floor((Math.random() * 6) + 1);
    
 	 return total;
  }
	}
  //
  // THIS FUNCTION PRODUCES THE RESULT WHEN THE ON-SCREEN BUTTON IS PRESSED
  //
  function playGame () {
  
 	 //document.open ();
 	 document.write ('1 was rolled' + totalArray[0] ' times.');
 	 document.write ('2 was rolled' + totalArray[1] ' times.');
 	 document.write ('3 was rolled' + totalArray[2] ' times.');
 	 document.write ('4 was rolled' + totalArray[3] ' times.');
 	 document.write ('5 was rolled' + totalArray[4] ' times.');
 	 document.write ('6 was rolled' + totalArray[5] ' times.');
 	 document.close ();
  }
	</script>
</head>


<body>
	<form 
  action="" 
  method="get" 
  name="dieForm">
  
  <input 
 	 name="rollButton" 
 	 type="button"
 	 value="Roll the Die"
 	 onclick = "playGame();">
	</form>
</body>
</html>



Please please tell me where i'm going wrong.

Thanking you all in advance
Trout69

Is This A Good Question/Topic? 0
  • +

Replies To: Javascript - Simulate Roll of dice

#2 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1877
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: Javascript - Simulate Roll of dice

Posted 25 June 2005 - 10:53 AM

It looks like you're off to a good start, there are a few small things that are missing. First, you are calling playGame() from the onclick action. In the playGame function you aren't actually "doing" anything, just ouputting the variables.

You need to take this piece of code:
for (var count = 1; count <= 1000; count = count + 1)
{
 //
 // THIS FUNCTION SIMULATES THE ROLL OF A SINGLE 6-SIDED DIE

 function rollDie () {
 
  var total = 0;
 
  total = Math.floor((Math.random() * 6) + 1);
   
  return total;
 }
}


and move the rollDie function out of the for loop. Inside the for loop should be this code:
  var total = (rollDie()-1);
 
 totalArray[total] = totalArray[total] + 1;

This will cause the die to roll and add the result to your array. Right now you have it outside any loops so it only executes once. And your for loop is just defining the function over and over, it's not actually being called.

I hope that helps, we try not to just write the code for you, so let us know where you're at with the info I've given you and we'll go from there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1