Uncaught TypeError: Can not read property '3' of undefined


I have a problem in javascript and I do not see how to solve it. I have looked at other people with a similar error message, and what it looks like is that a property does not have an object, but in my case it does (or at least I think so)

The function that gives the error is the following:

function Winner(id,r,c)
  if (r>=0 && r<=g_num_rows-4 &&
        g_table_board.DATA[r  ][c]==id &&
        g_table_board.DATA[r+1][c]==id &&
        g_table_board.DATA[r+2][c]==id &&
      return true;

  return false

id is 1, r and c are 3, g_num_rows is 7

g_table_board is defined as follows:

function TableBoard(r,c)
    this.NR = r;
    this.NC = c;

    this.DATA = new Array(this.NR);
    for (var i=0; i<this.NR; i++)
        this.DATA[i] = new Array(this.NC);
        for (var j=0; j<this.NC; j++)
          this.DATA[i][j] = 0;

and has been initialized as:

g_table_board = new TableBoard(7,7);

the call to Winner that gives the error is


The error is given when evaluating the fourth condition:


Interestingly, using the chrome tools, when I put the following watch:


it gives me the correct value, but when putting:


undefined tells me

And if I put:


gives me the correct value

In other parts of the code I have similar conditions and it does not give me any problem, for example this condition works well:

  if (r>=3 && r<=g_num_rows-1 &&
      g_table_board.DATA[r-3][c]==id &&
      g_table_board.DATA[r-2][c]==id &&
      g_table_board.DATA[r-1][c]==id &&
      g_table_board.DATA[r  ][c]==id)
    return true;

Any ideas?

asked by R. Montoliu 15.11.2016 в 11:28

1 answer


I have found the error, r is a string, and then r + 1 is concatenating "3" and "1" instead of adding them.

answered by 15.11.2016 / 11:59