Click to See Complete Forum and Search --> : Array size limited?


mitchpowell
07-30-2003, 01:26 PM
I have a motivational quote file that works fine with about 90 quotes, but something goes wrong after I try to add a few more quotes, and no quote shows at all when it's called.

The array is in an external file. Like this:

var quotes=new Array();

quotes[0]='This is a quote.<P ALIGN="RIGHT"><i>- author of quote</i></P>'

quotes[1]='This is a quote.<P ALIGN="RIGHT"><i>- author of quote</i></P>'

quotes[2]='etc...'

And at the end of all the quotes in the same external file is this command:

var whichquote=Math.floor(Math.random()*(quotes.length));


Then to call the quotes into my document, I use:

<SCRIPT TYPE="TEXT/JAVASCRIPT", SRC="js/externalquotesfile.js"></SCRIPT> in the head, and

<SCRIPT>document.write(quotes[whichquote]);</SCRIPT> in the body.


So when I try to add quotes[91] and beyond, it stops working. Why is this? I tested a bunch of very short quotes without much punctuation at the end, and they worked. I was able to get the quotes when all they said was: This is the 91st quote, and This is the 95th quote, and so on, but somewhere in the last quotes the punctuation seems to crash the whole thing.

David Harrison
07-30-2003, 01:38 PM
Is there anything else to your script, or is that it, some quotes and a document.write?
If that is it then I doubt it's the length of the array that's the problem, I've got a prime number finder that can put over 500,000 numbers into an array if you leave it long enough.

mitchpowell
07-30-2003, 02:02 PM
Just as you see my original post. I could point you to the URL where it's all supposed to work and doesn't, and I could show you the last quotes (whose punctuation is syntactically correct), but yeah, that's it. It just doesn't like the quotes after 90 or so.

I didn't think there would be a size limitation, but maybe the Math function needs modifying? Or what?

Okay, so go to http://powelltownusa.com/index2.html to see where the daily quote is supposed to appear,
then download http://powelltownusa.com/js/motivatequote.js to see the entire quotes file with 93 quotes in it.


I just did a chmod 755 on the file. Maybe that's what it needed.

Fang
07-30-2003, 02:33 PM
You have a carriage return on line 185 (quotes[90])

mitchpowell
07-30-2003, 05:04 PM
Thank you!

I now know about hard returns in a JavaScript file!
It was Dreamweaver that was letting it through.
Well, and TextPad too!
Oh yeah, and me!

Thanks again!

angrytuna
07-30-2003, 06:06 PM
Just to be nitpicky, with this line:

Math.floor(Math.random()*(quotes.length));

If Math.random() returns 1, the resultant index will go out of bounds. Not a big deal in javascript, but you might prefer:

Math.floor(Math.random()*(quotes.length-1));

mitchpowell
07-31-2003, 02:16 AM
Thanks. I didn't think it through.
My brother sells wine in Eugene.
http://www.chateaulorane.com/Qstore/Qstore.cgi