padarom
padarom

Reputation: 3648

Javascript TypeError: Array is undefined

to test some things in javascript I am building a small minesweeper.

The following code is the initialization of the two-dimension array P.field. Afterwards a number of random fields are getting filled with an x, symbolizing that there's a mine on this field.

P.field = new Array(num);
for (var i = 0; i < 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 < 10) {
    var found = false;
    var randomnumber = Math.ceil(Math.random()*(num*num-1));
    for (var i = 0; i < arr.length; i++)
    if (arr[i] == randomnumber) { found = true; break; }
    if (!found) arr[arr.length] = randomnumber;
}

for (var i = 0; i < arr.length; i++) {
    P.field[ Math.floor(arr[i]/num) ][ Math.floor(arr[i]-Math.floor(arr[i]/num)*num)-1 ] = 'x';
}

However, in my algorithm for counting the mines in surrounding fields, I get the console error TypeError: P.field[(r+1)] is undefined. Every field (except of those from the last row) returns this error, which is something I can't quite understand.

P.field[rows][columns] has a length of 10 per dimension in my tests ([10][10]). When I try to get the value of P.field[9][0] to P.field[9][9] there's nothing wrong. However when I adress any smaller row, this exception kicks in (P.field[0 + 1][0], P.field[3 + 1][6], and what so ever)...

I hope someone can tell me why.

edit

More 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;
},

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.

Upvotes: 0

Views: 6014

Answers (1)

Doug
Doug

Reputation: 3312

Looking at the code you've provided versus the errors you've had thrown, I'm skeptical about your suspected cause as the code used does indeed generate the correct set of arrays.

I suspect it may be a slightly simpler issue, the example generation code you've provided uses:

P.field = new Array(num);

which has P as a capital, whereas the error that you've had thrown uses it as lowercase:

TypeError: p.field[(r+1)] is undefined

Are you sure you haven't accidentally just used the incorrect case when testing?

Upvotes: 1

Related Questions