What is wrong with array.concat in this small example

F

F. Da Costa

Hi,

Could it be correct that the following code does *not* work because i'm not
using the var arr = new Array("a","b","c"); methodology??

Read through
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/array.html#1194827
but there was no mention of particular constructors having to be used.

And if so how does one resolve it (if resolvable)?

TIA
Fermin DCG

==========================
var famname = new Array();
famname[0] = "Jan 0 Egil";
famname[1] = "To1ve";
famname[2] = "He2ge";
famname[3] = "Sta3le";
famname[4] = "Kai 4Jim";
famname[5] = "Bor5ge";

var famname2 = new Array(6);
famname2[0] = "Jan Egil2";
famname2[1] = "Tove2";
famname2[2] = "Hege2";
famname2[3] = "Stale2";
famname2[4] = "Kai Jim2";
famname2[5] = "Borge2";

famname = famname.concat(famname2); *Does not produce the concatenation!*

for (var i=0; i<6; i++) {
document.write(famname + "<br />");
}

for (var i=0; i<6; i++) {
document.write(famname2 + "<br />");
}
 
M

McKirahan

F. Da Costa said:
Hi,

Could it be correct that the following code does *not* work because i'm not
using the var arr = new Array("a","b","c"); methodology??

Read through
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/ar
ray.html#1194827
but there was no mention of particular constructors having to be used.

And if so how does one resolve it (if resolvable)?

TIA
Fermin DCG

==========================
var famname = new Array();
famname[0] = "Jan 0 Egil";
famname[1] = "To1ve";
famname[2] = "He2ge";
famname[3] = "Sta3le";
famname[4] = "Kai 4Jim";
famname[5] = "Bor5ge";

var famname2 = new Array(6);
famname2[0] = "Jan Egil2";
famname2[1] = "Tove2";
famname2[2] = "Hege2";
famname2[3] = "Stale2";
famname2[4] = "Kai Jim2";
famname2[5] = "Borge2";

famname = famname.concat(famname2); *Does not produce the concatenation!*


Your using "famname" as an array and as a string. Try:

var famname3 = famname.concat(famname2);
 
F

F. Da Costa

McKirahan said:
Hi,

Could it be correct that the following code does *not* work because i'm
not

using the var arr = new Array("a","b","c"); methodology??

Read through

http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/ar
ray.html#1194827

but there was no mention of particular constructors having to be used.

And if so how does one resolve it (if resolvable)?

TIA
Fermin DCG

==========================
var famname = new Array();
famname[0] = "Jan 0 Egil";
famname[1] = "To1ve";
famname[2] = "He2ge";
famname[3] = "Sta3le";
famname[4] = "Kai 4Jim";
famname[5] = "Bor5ge";

var famname2 = new Array(6);
famname2[0] = "Jan Egil2";
famname2[1] = "Tove2";
famname2[2] = "Hege2";
famname2[3] = "Stale2";
famname2[4] = "Kai Jim2";
famname2[5] = "Borge2";

famname = famname.concat(famname2); *Does not produce the concatenation!*



Your using "famname" as an array and as a string. Try:

var famname3 = famname.concat(famname2);
That did the trick indeed.
Makes me wonder from time 2 time.

Thx a lot

Cheers
 
F

F. Da Costa

Grant said:
:

Hi,

Could it be correct that the following code does *not* work because i'm not
using the var arr = new Array("a","b","c"); methodology??

Read through
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/array.html#1194827
but there was no mention of particular constructors having to be used.

And if so how does one resolve it (if resolvable)?

TIA
Fermin DCG

==========================
var famname = new Array();
famname[0] = "Jan 0 Egil";
famname[1] = "To1ve";
famname[2] = "He2ge";
famname[3] = "Sta3le";
famname[4] = "Kai 4Jim";
famname[5] = "Bor5ge";

var famname2 = new Array(6);
famname2[0] = "Jan Egil2";
famname2[1] = "Tove2";
famname2[2] = "Hege2";
famname2[3] = "Stale2";
famname2[4] = "Kai Jim2";
famname2[5] = "Borge2";

famname = famname.concat(famname2); *Does not produce the concatenation!*

for (var i=0; i<6; i++) {
document.write(famname + "<br />");
}

for (var i=0; i<6; i++) {
document.write(famname2 + "<br />");
}



Of COURSE it concatenates the arrays, but if you look at your "test code" you are simply looping
through 6 elements of the first array and 6 elements of the second array and outputting them.

:) The infamous middle of the night error.
Yep you are very correct. Obviously resolved this whole thing and knee-high
in other muck again.

Thx for the reaction anyway.
If you modify your test code to actually output all of the array values, you'll see that famname
contains all the entries from both famname and famname2:

document.write("Contents of famname<br />");
for (var i=0; i<famname.length; i++) {
document.write(famname + "<br />");
}

document.write("Contents of famname2<br />"):
for (var i=0; i<famname2; i++) {
document.write(famname2 + "<br />");
}

Better yet:

document.write("<p>Contents of famname<br />" + famname.join("<br />") + "</p>");
document.write("<p>Contents of famname2<br />" + famname2.join("<br />") + "</p>");

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
* http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html
* Internet Explorer DOM Reference available at:
* http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp
* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top