...

View Full Version : how to return 2 dimensional array



lolena1888
07-12-2004, 08:10 PM
hello,

I'm rather new in JS. Can somebody help me?.:rolleyes:
I try to return 2 demensional array from the function like this:
<SCRIPT LANGUAGE=Javascript 1.2>
function 1
{
elements=new Array();
for (i=0;i<7;i++)
{ function2(elements[i],i)
}
return (elements);
}

function 2(str,num)
{ elements[num]=new Array();
for (i=0;i<7;i++)
{ elements[elements[num][i]=i;
}
}

var na=new Array(new Array());
na=function1;
for (i=0;i<7;i++)
{ for (j=0;i<7;i++)
{ alert(na[i][j]);}
}
//As I understand, elements is global array...
And it gives me an error (elements, na are empty or are not an objects). Could you tell me where my problem is?

ez4me2c3d
07-12-2004, 09:13 PM
this may not solve your problem but I always though a 2-d array was such.


myArray = new Array(
new Array("Item 1a", "Item 1b"),
new Array("Item 2a", "Item 2b")
);

//the you reference one item like this

sItem = myArray[1][1];

//now sItem should be "Item 2b"

I'm up for learning something new, so go ahead and tell me im wrong if im wrong.

sad69
07-12-2004, 09:21 PM
From what I can tell, elements is actually declared in function1() (rather function 1... why do you have spaces in your function names? That's "illegal").

That's what I see as being wrong anyway..


var elements=new Array();

function function1() {
for (var i=0;i<7;i++) {
function2(elements[i],i);
}

return (elements);
}

function function2(str,num) {
elements[num]=new Array();

for (var i=0;i<7;i++) {
elements[num][i]=i;
}
}

var na=new Array(new Array());
na=function1();
for (var i=0;i<7;i++) {
for (var j=0;j<7;j++) {
alert(na[i][j]);
}
}



I've highlight your errors that I saw in red. Hopefully that works for you.
Sadiq.

kansel
07-12-2004, 09:21 PM
Here is some problem with your script

1: function 1
2: {
3: elements=new Array();
4: for (i=0;i<7;i++)
5: { function2(elements[i],i)
6: }
7: return (elements);
8: }
9:
10: function 2(str,num)
11: { elements[num]=new Array();
12: for (i=0;i<7;i++)
13: { elements[elements[num][i]=i;
14: }
15: }
16:
17: var na=new Array(new Array());
18: na=function1;
19: for (i=0;i<7;i++)
20: { for (j=0;i<7;i++)
21: { alert(na[i][j]);}
22: }

1: this is an invalid command. it appears as though you are trying to declare a function. use this: function function1()
5: pass by reference, not by value. try: function2(elements,i);
10: same deal. use: function function2()
10: you appear to be passing an unused variable str.
11: do you mean str[num] = new Array();
13: you've got too many [ and not enough ]. (three opening square braces vs. two closing square braces.
13: do you mean str[num][i] = i;
15: you're not returning any value. the work you did in function2() will be lost.
17: this is an invalid call to Array(). Array() expects an integer or a comma-separated list of values.
18: you are replacing your newly declared array with a function. JavaScript will allow you to reuse the name function1, but your array is lost in the process.
20: if you're trying to loop j, you need to have j in all three spots in the for structure: for(j=0;j<7;j++)
21: sidestepping a potential headache. there are better ways to output than alert(). you stand to see 49 annoying alert() popups (along with the accompanying noise if your OS does that) if this actually works.

Go over these notes. Think about the work you're trying to do. Let me know if you have further questions.

darn it!! beaten again.

Willy Duitt
07-12-2004, 09:29 PM
darn it!! beaten again.

Both here and abroad (http://www.webdeveloper.com/forum/showthread.php?threadid=39356). :eek:

Well, I suppose you needed the practice.
Nice summation tho....

.....Willy

sad69
07-12-2004, 09:40 PM
this may not solve your problem but I always though a 2-d array was such.


myArray = new Array(
new Array("Item 1a", "Item 1b"),
new Array("Item 2a", "Item 2b")
);

//the you reference one item like this

sItem = myArray[1][1];

//now sItem should be "Item 2b"

I'm up for learning something new, so go ahead and tell me im wrong if im wrong.

Like everything else in computing, there's usually more than one way to accomplish something. n-dimensional arrays is one of them.

ez4me2c3d, you've pointed out one way. Another way is like this:


var myArray = new Array();

myArray [0] = new Array();
myArray [0][0] = "Item 1a";
myArray [0][1] = "Item 1b";

myArray [1] = new Array();
myArray [1][0] = "Item 2a";
myArray [1][1] = "Item 2b";

var sItem = myArray[1][1];

Willy Duitt
07-12-2004, 09:48 PM
Or using bracket notation:



var myArray = [["Item 1a" , "Item 1b"],
["Item 2a" , "Item 2b"]]



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum