Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript TypeError: Array is undefined
    text
    copied!<p>to test some things in javascript I am building a small minesweeper.</p> <p>The following code is the initialization of the two-dimension array <code>P.field</code>. Afterwards a number of random fields are getting filled with an <code>x</code>, symbolizing that there's a mine on this field.</p> <pre><code>P.field = new Array(num); for (var i = 0; i &lt; P.field.length; i++) P.field[i] = new Array(num); $.each(P.field, function(index, key) { $.each(key, function(i, k) { P.field[index][i] = '-'; }); }); var arr = []; while (arr.length &lt; 10) { var found = false; var randomnumber = Math.ceil(Math.random()*(num*num-1)); for (var i = 0; i &lt; arr.length; i++) if (arr[i] == randomnumber) { found = true; break; } if (!found) arr[arr.length] = randomnumber; } for (var i = 0; i &lt; arr.length; i++) { P.field[ Math.floor(arr[i]/num) ][ Math.floor(arr[i]-Math.floor(arr[i]/num)*num)-1 ] = 'x'; } </code></pre> <p>However, in my algorithm for counting the mines in surrounding fields, I get the console error <code>TypeError: P.field[(r+1)] is undefined</code>. Every field (except of those from the last row) returns this error, which is something I can't quite understand.</p> <p><code>P.field[rows][columns]</code> has a length of 10 per dimension in my tests ([10][10]). When I try to get the value of <code>P.field[9][0]</code> to <code>P.field[9][9]</code> there's nothing wrong. However when I adress any smaller row, this exception kicks in (<code>P.field[0 + 1][0]</code>, <code>P.field[3 + 1][6]</code>, and what so ever)...</p> <p>I hope someone can tell me why.</p> <p><strong>edit</strong></p> <p>More code:</p> <pre><code>onReady: function() { $('#sweeper table').on('click', 'td', function(e) { var row = $(this).parent().attr('class'); // Class represents the index of the array var column = $(this).attr('class'); // Class represents the index of the array P.openField(row, column, $(this)); }); }, openField: function(r, c, e) { if ( P.field[r][c] == 'x' ) { e.addClass('mine'); } else { e.html( P.surroundingMineCount(r, c) ); e.addClass('opened'); } }, surroundingMineCount: function(r, c) { var count = 0; if ( P.field[r][c-1] == 'x' ) count++; if ( P.field[r-1][c-1] == 'x' ) count++; if ( P.field[r][c+1] == 'x' ) count++; if ( P.field[r-1][c] == 'x' ) count++; if ( P.field[r-1][c+1] == 'x' ) count++; if ( P.field[r+1][c] == 'x' ) count++; if ( P.field[r+1][c-1] == 'x' ) count++; return count; }, </code></pre> <p>Right now I have no validation if the r+1 or r-1 is actually a valid index for that array (I had one in, but removed it for testing). However that can't really be the error, because I even get errors in the middle of the table.</p>
 

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