# Begginer - Problem with Matrix

3 posts Page 1 of 1

## Begginer - Problem with Matrix

by zearomm » Wed May 28, 2014 4:14 pm

The code just keep giving me 6 or 8 independent of the position i'm trying to access.

Code: Select all
`var valueMatrix = 0;var matrix = [[0,0],[0,0],[0,0]];   matrix[0,0,0] = 1;   matrix[0,1,0] = 2;   matrix[0,0,1] = 3;   matrix[0,1,1] = 4;   matrix[1,0,0] = 5;   matrix[1,1,0] = 6;   matrix[1,0,1] = 7;   matrix[1,1,1] = 8;         function test()   {    valueMatrix = matrix[0,0,0];   alert(valueMatrix);  }`

I dont know how to solve this...

(sorry for my poor english)

zearomm

• Posts: 2
• Joined: Mon May 12, 2014 2:50 pm

## Re: Begginer - Problem with Matrix

by nanoverso » Wed May 28, 2014 10:16 pm

Looks like you are using an equivocated initialization code for a 3x3 matrix who have 3 elements per row, and a wrong access sintax (correct is [i][i]) and indexes for these elements (indexed 0, 1 and 2). Try this:

Code: Select all
`var valueMatrix = 0;var matrix = [[0,0,0],[0,0,0],[0,0,0]];matrix[0][0] = 1;matrix[0][1] = 2;matrix[0][2] = 3;matrix[1][0] = 4;matrix[1][1] = 5;matrix[1][2] = 6;matrix[2][0] = 7;matrix[2][1] = 8;   matrix[2][2] = 9;valueMatrix = matrix[1][2]; // 6alert(valueMatrix);`

Checkout my Mario blog (Portuguese): Jogos do Mario Bros.

Last edited by nanoverso on Thu May 16, 2019 4:59 am, edited 1 time in total.

nanoverso

• Posts: 254
• Joined: Sat May 10, 2014 12:08 am
• Location: Brazil

## Re: Begginer - Problem with Matrix

by zearomm » Sat May 31, 2014 10:14 pm

Yeah! Worked.

Thanks for the support!

zearomm

• Posts: 2
• Joined: Mon May 12, 2014 2:50 pm