Exploring an out of bounds check issue, I'm struggling with managing undefined
values within a 2d array.
My attempted solutions include verifying if both the column and row index are present using if(!arr[x][y])
, checking if both are undefined with if(arr[x][y] === undefined)
, and finally ensuring that the indexes are within range (the array is 10x10).
Unfortunately, none of these approaches have been successful. Even when combining the conditions, I still encounter
TypeError: Cannot set property '' of undefined
.
Is there a workaround for this situation?
Edit:
The TypeError
occurs at
if (arr[x + i * dx][y + i * dy] === undefined)
, specifically at [y + i * dy]
where x
and y
represent the column and row indexes, while dx
and dy
are either 0 or 1 to enable vertical or horizontal checks.
No errors are encountered when verifying vertically, only horizontally triggers the TypeError
.