Page 1 of 1

Accessing double key array from JS

Posted: 18 July 2024, 20:56
by tony58uk
Hi all,

I'm having real problems. I want to test if a value exists at a certain location within a double-key array.

My code is as follows (JS)

Code: Select all

if ((board[(die_x + 1)][die_y] === undefined) &&
                        (board[(die_x + 2)][die_y] === undefined) &&
                        (board[(die_x + 3)][die_y] === undefined)) {

                            return_value = true;

                        }
                        
I keep getting type errors (TypeError: Cannot read properties of undefined (reading '6'))

I'm missing something silly but cannot work it out!!!

Thanks

Re: Accessing double key array from JS

Posted: 18 July 2024, 22:51
by robinzig
Presumably the outer `board` array is not long enough for the first index (`die_x + 2` and so on) to exist at some point.

Beyond that, which is obvious from the error message, it's on you to debug because we've got no idea what's in that array or what values `die_x` etc might have.

Re: Accessing double key array from JS

Posted: 19 July 2024, 09:32
by RicardoRix
use the debugger; statement (put it just before the code to analyse) and the F12 console. You can now single-step through your code.

Re: Accessing double key array from JS

Posted: 19 July 2024, 13:54
by AxeCrazy
I had a simulair issue before.
die_x + 1 =>
Are you sure die_x is an integer?
I always put a parseInt() round it these days
if die_x = 4 and you add 1 it might result in 41 (stingwise addition) in stead of 5 which you assume it would be

Re: Accessing double key array from JS

Posted: 22 July 2024, 22:49
by tony58uk
Okay thanks all, I'll have a look at some of those suggestions tomorrow.