Update this program is supposed to keep track of the number of times each side is rolled not each roll itself.
I am working on a javascript exercise that needs to take two dice, roll them 1000 times and keep track of each roll. I thought the best way to do it was to use an array but I am not exactly sure if I am going in the right direction with what I am doing. I just need a little help in pointing me in the right direction to get this to work because right now it doesnt output anything. Thanks here is my code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Exercise Five</title>
<script type="text/javascript">
function Die(){
this.roll = function(){
this.sides = 0;
return parseInt((Math.random( ) * 1000) % this.sides) + 1;
}
}
</script>
</head>
<body>
<script type="text/javascript">
var dieOne = new Die();
var dieTwo = new Die();
var rollOne = 0;
var rollTwo = 0;
var roll_value = new Array();
var arrayPlace = 0;
for(var i = 0; i < 1000 ; i++){
rollOne = dieOne.roll();
dieOne.sides = 10;
rollTwo = dieTwo.roll();
dieTwo.sides = 10;
arrayPlace = (rollOne + rollTwo) - 2;
roll_value[arrayPlace]++;
}
for( var i = 0; i < roll_value.length; i++){
document.writeln(roll_value[i]);
}
</script>
</body>
</html>
dieOne.sides = 10;You probably want to do that before you roll the die, not after. And no need to reset this inside of the loop every time.var sides = 0; return parseInt((Math.random( ) * 1000) % sides) + 1;Looks like it would divide by zero every time.