1

I've got this problem. My function seems to check outside the borders of my 10x10 board. It's made to return the number of mines around a Tile[row, column] but I get :

minesweeper.js:152 Uncaught TypeError: Cannot read property '0' of undefined
    at getAdjacentMines (minesweeper.js:152)

Here is my function, thanks everyone for your time.

function getAdjacentMines(row, col) {
    let count = 0;
    let minRow = row - 1;
    let maxRow = row + 1;
    let minCol = col - 1;
    let maxCol = col + 1;

    if (minRow < 0) minRow = 0;
    if (maxRow > board.length) maxRow = board.length - 1;
    if (minCol < 0) minCol = 0;
    if (maxCol > board.length) maxCol = board.length - 1;


    for (row = minRow; row <= maxRow; row++) {
        for (col = minCol; col <= maxCol; row++) {
            if (board[row][col].boom === true) count++;
        }
    }

    return count;
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95

1 Answers1

2

Welcome to Stack overflow!

you seem to have a typo on this section

for (row = minRow; row <= maxRow; row++) {
    for (col = minCol; col <= maxCol; row++) {
        if (board[row][col].boom === true) count++;
    }
}

That needs to be:

//                                 v here
for (col = minCol; col <= maxCol; col++) {

Not:

 for (col = minCol; col <= maxCol; row++) {

That means that 'row' will keep increasing until it reaches an index of 10, then it will try to read 'col' (which will be 0) of row 10 (which will be undefined)

Zock77
  • 951
  • 8
  • 26