Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>What you're doing</strong></p> <p>It seems like you're misunderstanding the syntax of what you're doing. It is currently equivalent to</p> <pre><code>var dieOne = Math.floor(Math.random() * 6 + 1); var dieTwo = Math.floor(Math.random() * 6 + 1); var foo; foo = dieOne; // a number foo = [foo]; // an array with one number in it foo = foo[dieTwo]; // probably undefined, unlikely case of `dieTwo = 0` // which would give back `dieOne` dice[foo] = dice[foo] + 1; // most likely trying to add 1 to property `undefined` </code></pre> <p>Then later you're doing</p> <pre><code>foo = [1]; // an array length 1 foo = foo[3]; // undefined, it doesn't have an item here foo = dice[foo]; // = dice[undefined] = undefined alert(foo); // alerting "undefined" </code></pre> <hr> <p><strong>What you probably want to be doing</strong></p> <p>It looks like you actually want an <em>Array</em> of <code>6</code> <em>Arrays</em> each of <code>6</code> <em>Numbers</em>; construct your favourite way</p> <pre><code>var dice = [ [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0] ]; </code></pre> <p>Then for your loop to be </p> <pre><code>var dieOne, dieTwo, i; for(i = 0; i &lt; 30000; ++i) { dieOne = Math.floor(Math.random() * 6); // indices start at 0 and dieTwo = Math.floor(Math.random() * 6); // 6 of them means max is 5 dice[dieOne][dieTwo]++; // count the brackets here.. } </code></pre> <p>then say you want to know how many times you had <em>dieOne = 1, dieTwo = 3</em>, you would look at</p> <pre><code>dice[1][3]; // count the brackets again. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload