Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random text write almost 100%, cannot find bug...

Status
Not open for further replies.

JSCboulder

Technical User
Oct 6, 2008
6
DE
I have a simple script which is randomly getting and writing text from 16 chunks of text. It's pasted below. It writes all of them except one - the last one, #16, and I can't find out why... I've already zapped germlins out of the text. Anyone see why the last line only gives this error:
undefined

?
thanks
JSC

script: (writes to a defined div)
var myquotes = new Array ();
myquotes[1]="<span>Man has nothing else to do but surrender in deep trust, in deep love. Dont be a doer, just surrender. Let there be a let go.</span>&nbsp;&nbsp;&nbsp;-&nbsp;Osho";
myquotes[2]="<span>Meditation is possible only when you are unmotivated, you dont have any desire. When there is no desire, there is meditation. Meditation is a state of desirelessness.</span>&nbsp;&nbsp;&nbsp;-&nbsp;Osho";
myquotes[3]="<span>That\'s what meditation is - just getting beyond the mind and beyond time, and you have entered into eternal silence, into eternal life.</span>&nbsp;&nbsp;&nbsp;-&nbsp;Osho";
myquotes[4]="<span>The mind is constantly talking. If the inner talk can drop even for a single moment you will be able to have a glimpse of no-mind. That\'s what meditation is all about.</span>&nbsp;&nbsp;&nbsp;-&nbsp;Osho";
myquotes[5]="<span>Meditation is a way of settling in oneself, at the innermost core of your being. Once you have found the center of your existence, you will have found both roots and wings.</span>&nbsp;&nbsp;&nbsp;-&nbsp;Osho";
myquotes[6]="<span>You cannot desire meditation, because meditation happens only when there is no desire.</span>&nbsp;&nbsp;&nbsp;-&nbsp;Osho";
myquotes[7]="<span>Whenever some bliss happens, trust it, and go in that direction. If you can follow bliss, you will never go astray, you will be following nature.</span>&nbsp;&nbsp;&nbsp;-&nbsp;Osho";
myquotes[8]="<span>My whole approach towards life is that of total acceptance, is that of celebration, not of renunciation.</span>&nbsp;&nbsp;&nbsp;-&nbsp;Osho";
myquotes[9]="<span>Life is a celebration because it is an adventure.</span>&nbsp;&nbsp;&nbsp;-&nbsp;Osho";
myquotes[10]="<span>I am neither for anything, nor against anything. My whole approach is that you should find a deep, relaxed contentment with yourself and existence.</span>&nbsp;&nbsp;&nbsp;-&nbsp;Osho";
myquotes[11]="<span>Faith is dangerous, never cheap. You will have to put your whole life at stake. It needs courage, but only a courageous person can be religious.</span>&nbsp;&nbsp;&nbsp;-&nbsp;Osho";
myquotes[12]="<span>Meditation is just a courage to be silent and alone.</span>&nbsp;&nbsp;&nbsp;-&nbsp;Osho";
myquotes[13]="<span>Unless a man learns how to create, he never becomes a part of existence, which is constantly creative. By being creative one becomes divine; creativity is the only prayer.</span>&nbsp;&nbsp;&nbsp;-&nbsp;Osho";
myquotes[14]="<span>To know life in its insecurity is to know life in its immense beauty, is to know life in its authenticity.</span>&nbsp;&nbsp;&nbsp;-&nbsp;Osho";
myquotes[15]="<span>From birth to death man is a long sleep, sometimes dreaming with eyes closed, sometimes dreaming with eyes open, but dreaming all the same, all the time.</span>&nbsp;&nbsp;&nbsp;-&nbsp;Osho";
myquotes[16]="<span>Those otherworldly desires were just as foolish as the worldly desires.</span>&nbsp;&nbsp;&nbsp;-&nbsp;Osho";
var i = Math.floor(16*Math.random())

document.write(myquotes);
 
The Problem seems to be that your random number generation is capable of generating 0's (zeros) which since you don't have an item in the 0 index of your array would produce the undefined error. Additionally your generator will never generate the number 16 so your 16th quote will never be displayed.

The simple fix is to add a plus one to your generator so it never produces a 0, and also so it can produce a 16.

Code:
var i = Math.floor((16*Math.random())[red]+1[/red])

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top