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

2

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 &&
        g_table_board.DATA[r+3][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

Winner(1,3,3)

The error is given when evaluating the fourth condition:

g_table_board.DATA[r+1][c]==id

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

g_table_board.DATA[r]

it gives me the correct value, but when putting:

g_table_board.DATA[r+1] 

undefined tells me

And if I put:

g_table_board.DATA[r-1]

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
source

1 answer

1

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
source