MyArray.length returns undefined...

D

Dag Sunde

....while MyArray.toString() returns the expected value...

I have a function that returns an array:

function getProducts( auctionID )
{
var prodCount = 0;
var prodArray = new Array()
for( i=0; i < products.length; i++ )
{
if( products[0] == auctionID )
{
prodArray[prodCount] = new Array(1);
prodArray[prodCount][0] = products[1];
prodArray[prodCount][1] = products[2];
prodCount++;
}
}
return prodArray;
}

and I use it here:

var productArray
productArray = getProducts(idxAuction);

// now, productArray should hold the array returned from
// my function above.
var numProducts = productArray.lenght;
var topInfo = document.getElementById("topInfoLayer");

topInfo.innerHTML = numProducts;
...

This prints "undefined" in my topinfo layer???

Anyone?

TIA...

--
Dag.
EMail: echo lvjnugnwewn | tr "vjenlgwu" "[email protected]"

"Anyone who cannot cope with mathematics is not fully human.
At best he is a tolerable subhuman who has learned to wear
shoes, bathe and not make messes in the house."
--Lazarus Long, "Time Enough for Love", (Robert A. Heinlein)
 
G

George Jempty

Dag said:
...while MyArray.toString() returns the expected value...

I have a function that returns an array:

function getProducts( auctionID )
{
var prodCount = 0;
var prodArray = new Array()
for( i=0; i < products.length; i++ )
{
if( products[0] == auctionID )
{
prodArray[prodCount] = new Array(1);
prodArray[prodCount][0] = products[1];
prodArray[prodCount][1] = products[2];
prodCount++;
}
}
return prodArray;
}

and I use it here:

var productArray
productArray = getProducts(idxAuction);

// now, productArray should hold the array returned from
// my function above.
var numProducts = productArray.lenght;


could be the above typo. it should be productArray.length.

however I don't think this is it anyway. Javascript "object" arrays, or
"associative" arrays -- the ones WITHOUT numeric indexes: don't support
the length property.
 
D

Dag Sunde

George Jempty said:
<snipped/


could be the above typo. it should be productArray.length.

however I don't think this is it anyway. Javascript "object" arrays, or
"associative" arrays -- the ones WITHOUT numeric indexes: don't support
the length property.

It was the typo (Damn, I *have* to find those glasses)!

Thanks a lot, George!
 
M

Michael Winter

...while MyArray.toString() returns the expected value...

I have a function that returns an array:

function getProducts( auctionID )
{
var prodCount = 0;
var prodArray = new Array()

You forgot the terminating semi-colon (not strictly necessary, but good
practice).
for( i=0; i < products.length; i++ )

You should declare 'i' local: use the var keyword.
{
if( products[0] == auctionID )
{
prodArray[prodCount] = new Array(1);


You might as well create an empty array or, if you must, at least create
it with the correct initial size (2).
prodArray[prodCount][0] = products[1];
prodArray[prodCount][1] = products[2];
prodCount++;
}
}
return prodArray;
}

and I use it here:

var productArray
productArray = getProducts(idxAuction);


This *will* return an array, as long as no errors occur during execution
of getProducts().
// now, productArray should hold the array returned from
// my function above.
var numProducts = productArray.lenght;

This appears to be an obvious typo, but the only thing that appears to
cause a problem. If there are no typos in your actual code (they may not
actually appear as errors!), you'll have to post it verbatim.
var topInfo = document.getElementById("topInfoLayer");

Do you check for support of the getElementById method? That is, does this
appear anywhere?

if( document.getElementById ) {
// getElementById supported
topInfo.innerHTML = numProducts;
...

Though you use arrays a lot here, you should not forget that JavaScript is
an object-oriented language, and some of your arrays, such as those that
describe products, might be better represented as objects.

You can read about objects in Netscape's JavaScript Guide (watch for URL
wrap):
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/obj.html

Douglas Crockford also has some things to say about objects, amongst other
things about the JavaScript language:
http://www.crockford.com/javascript/survey.html

Mike


Netscape's JavaScript Guide, v1.5:
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/

Douglas Crockford's website:
http://www.crockford.com/
 

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
473,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top