0

Its basically an out of bounds check, tho' I'm having issues understanding how to deal with undefined values in a 2d array.

What I've tried so far is checking if both the column and the row index exist if(!arr[x][y]), checking if both are undefined if(arr[x][y] === undefined) and finally checking if the indexes are bigger then the array length - 1 ( the array is 10x10 ).

None of the above have worked, even when putting the conditions togheter I still get

TypeError: Cannot set property '' of undefined.

Is there a way to deal with this ?

Edit:

The TypeError appears at if (arr[x + i * dx][y + i * dy] === undefined) more precisely at [y + i * dy] where x and y are the column and row indexes, dx and dy are either 0 or 1 values so it can check either vertically or horizontally.

If I check vertically, I dont get any errors, its only when I check horizontally that I encounter the TypeError.

As above so below
  • 449
  • 1
  • 5
  • 12
  • That's not how 2D arrays work. They're basically just arrays of arrays: arr[x][y] is (arr[x])[y]. – Denys Séguret Aug 24 '17 at 16:50
  • Can you show us how you define (or fill) `arr`? – ibrahim mahrir Aug 24 '17 at 16:59
  • @DenysSéguret My mistake. – As above so below Aug 24 '17 at 17:08
  • @TudorApostol, please edit your question, and put that code there. – trincot Aug 24 '17 at 17:10
  • @ibrahimmahrir https://jsbin.com/vuduvuvimu/edit?html,js,output – As above so below Aug 24 '17 at 17:12
  • @Tudor Apostol, please provide a [Complete, Minimal, and Verifiable](https://stackoverflow.com/help/mcve) example. It can be difficult to assist without having the code to reproduce your issue. – McHat Aug 24 '17 at 17:12
  • @TudorApostol, in the jsbin code you did not define `obj` anywhere. But please add the code *that produces the error* in your question, not in jsbin. Indent code using the `{ }` tool in the toolbar you get when you edit your question. – trincot Aug 24 '17 at 17:14
  • The error: `TypeError: Cannot set property '' of undefined` is caused because `x < 0 || x > 9` (or not even a number) or because one of the subarrays don't exist (probably removed). – ibrahim mahrir Aug 24 '17 at 17:16
  • + you should split the check `arr[x][y]` to `arr[x] && arr[x][y]` to be safe. – ibrahim mahrir Aug 24 '17 at 17:18
  • @trincot Hope the edit helps. – As above so below Aug 24 '17 at 17:44
  • @ibrahimmahrir Yes, the subarrays is where I'm facing this issue. How can I approach solving this issue ? – As above so below Aug 24 '17 at 17:45
  • You can check if the subscript are valid before accessing the element from the 2D array: `if (x + i * dx < 0 || 9 < x + i * dx || y + i * dy < 0 || 9 < y + i * dy || arr[x + i * dx][y + i * dy] === undefined) { /* Either the subscripts are invalid or the cell at (x + i * dx, y + i * dy) is not defined */ }`. – ibrahim mahrir Aug 24 '17 at 17:50
  • ... Itstill vague though. What are you trying to acheive? – ibrahim mahrir Aug 24 '17 at 17:52
  • @ibrahimmahrir Basically I'm randomizing values for both x and y so I can get a random arr[x][y] index and I want to check for `i` amount of neighboring indexes both on the right and on the bottom of the randomized initial index. – As above so below Aug 24 '17 at 17:59
  • @TudorApostol, please post code with which we can reproduce the problem. The error you get would not occur if the values of x, y, dx, dy are as you say, so there must be something going wrong. We can only see what, if you provide enough code for us to reproduce the problem. – trincot Aug 24 '17 at 19:51

1 Answers1

0

You should check like this instead:

if (arr[x + i * dx] === undefined || arr[x + i * dx][y + i * dy] === undefined)

This way, if the row is undefined, the second check is not performed and won't throw an error. This is possible thanks to shirt-circuit evaluation:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Short-circuit_evaluation

Slava Eremenko
  • 1,936
  • 1
  • 10
  • 9