开发者

Javascript program help

开发者 https://www.devze.com 2023-04-07 14:44 出处:网络
Update this program is supposed to keep track of the number of times each side is rolled not each roll itself.

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开发者_StackOverflow社区(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>


Sorry, see updated code below:

var dieOne = new Die();
var dieTwo = new Die();
var rollOne = 0;
var rollTwo = 0;
var roll_values = new Array();

for(var i = 0; i < 1000 ; i++){
  rollOne = dieOne.roll();
  rollTwo = dieTwo.roll();
  roll_values[i] = { 'first': rollOne,
                     'second': rollTwo,
                     'value': 0 };
  // 1. iterate through past roll_values[i].first, roll_values[i].second
  // in order to determine what you need to increment and by how much

  // 2. finally set roll_values[i].value = var_containing_value;
}

for( var i = 0; i < roll_value.length; i++){
  console.log(roll_value[i]);
}

And, another note, for debugging, console.log(whatever) is excellent and supported on most modern browsers (IE > 8, thanks Kolink). The console log will show up in the javascript/error console.

There were two problems: how you were storing the roll values and how you were incrementing your array. Both should now be fixed.


Try this:

  function Dice(sides) {
    this.sides = sides;
  }

  Dice.prototype.roll = function() {
    return (Math.random() * (this.sides + 1)) | 0 ;
  }

  var counts = [];
  var d1 = new Dice(6);
  var d2 = new Dice(6);
  var s;

  for (var i=1000; i; i--) {
    s = d1.roll() + d2.roll();
    counts[s] = typeof counts[s] == 'undefined'? 1 : ++counts[s];
  }

  // Show results
  for (var i=0, iLen=counts.length; i<iLen; i++) {
    console.log(i + ' : ' + counts[i]);
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消