Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: 2D Array version of the dice game (help with array counter)

1. ## 2D Array version of the dice game (help with array counter)

Hey guys, I'm new here, but I've been struggling with the code for this. I think it's a matter of where I'm putting my statements. The array is supposed to count how many times a combination of number has been rolled (ie. 1, 4 or 5, 3) For some reason nothing is being written into the array. My first for loop is supposed to run the dice toss however many times the user enters...Here's what I have.
Code:
```
for(var count = 0; count < roll; count++)
{
for(die1 in a)
{
for(die2 in a[die1])
{
die1 = Math.floor(1 + Math.random() * 6);
die2 = Math.floor(1 + Math.random() * 6);
++a[die1][die2];
}
}
}```
Any help would be appreciated.

• try this:

// ----- initializing the twodimensional array and sets each value to 0 .
a=new Array;
for (x = 1; x < 7; x++){
a[x] = new Array;
for (y = 1; y < 7; y++){
a[x][y] = 0;
}}
roll=eval(input);

for(var count = 0; count < roll; count++){
die1 = Math.floor(1 + Math.random() * 6);
die2 = Math.floor(1 + Math.random() * 6);
a[die1][die2]++;
}

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•