how to make an array with 2 dimensions?

D

Dave

Hi,

i tried this:
a=new Array()
for (i=1;i<=3;i++)
{
for (j=1;j<=2;j++)
a[i,j]=i+j
}

for (i=1;i<=3;i++)
{
for (j=1;j<=2;j++)
alert(a[i,j])
}

but i get 4,5,4,5,4,5 while i expect 2,3,3,4,4,5
Thanks
Dave
 
J

Janwillem Borleffs

Dave said:
Hi,

i tried this:
a=new Array()
for (i=1;i<=3;i++)
{
for (j=1;j<=2;j++)
a[i,j]=i+j
}

for (i=1;i<=3;i++)
{
for (j=1;j<=2;j++)
alert(a[i,j])
}

but i get 4,5,4,5,4,5 while i expect 2,3,3,4,4,5

a=[];
for (i=1;i<=3;i++) {
a = [];
for (j=1;j<=2;j++)
a[j] = i+j;
}

for (i=1;i<=3;i++) {
for (j=1;j<=2;j++)
alert(a[j]);
}


JW
 
M

Martin Honnen

Dave said:
Hi,

i tried this:
a=new Array()
for (i=1;i<=3;i++)
{
for (j=1;j<=2;j++)
a[i,j]=i+j
}

for (i=1;i<=3;i++)
{
for (j=1;j<=2;j++)
alert(a[i,j])
}

but i get 4,5,4,5,4,5 while i expect 2,3,3,4,4,5

You can only create an array of arrays with JavaScript
var a = new Array();
for (var i = 0; i < 5; i++) {
a = new Array();
for (var j = 0; j < 10; j++) {
a[j] = j;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,575
Members
47,207
Latest member
HelenaCani

Latest Threads

Top