Problem with Random Quote Generator

L

lharby

I'm hoping this is very simple. I am currently using a random quote
generator on our intranet.

I have 17 quotes, when I add in an 18th and change the makeArray
number the code seems not to work.

This works:

<script LANGUAGE="JavaScript">
function makeArray(len) {
for (var i = 0; i < len; i++) this = null;
this.length = len;
}
quotes = new makeArray(17); //This Number is a variable relating to
the Number of Quotes//
quotes[0] = "\"Imagination is more important than knowledge \" Albert
Einstein";
quotes[1] = "\"One should always play fairly when one has the winning
cards. \" Oscar Wilde"; ...............
.............
quotes[16] = "\"Well, if I called the wrong number, why did you answer
the phone? \" James Thurber";

function rand(n) {
seed = (0x015a4e35 * seed) % 0x7fffffff;
return (seed >> 16) % n;
}
var now = new Date()
var seed = now.getTime() % 0xffffffff
</script>

<script LANGUAGE="JavaScript">
var quoterand = quotes[rand(quotes.length)];
if (quoterand.length < 40) {
document.write(quoterand+"")
} else { document.write(quoterand)
}

</script>


All I od is add another quote, increase the makeArray and it goes
wrong. Any ideas?


TIA
Luke
 
E

Erwin Moller

lharby said:
I'm hoping this is very simple. I am currently using a random quote
generator on our intranet.

I have 17 quotes, when I add in an 18th and change the makeArray
number the code seems not to work.

This works:

<script LANGUAGE="JavaScript">
function makeArray(len) {
for (var i = 0; i < len; i++) this = null;
this.length = len;
}
quotes = new makeArray(17); //This Number is a variable relating to
the Number of Quotes//
quotes[0] = "\"Imagination is more important than knowledge \" Albert
Einstein";
quotes[1] = "\"One should always play fairly when one has the winning
cards. \" Oscar Wilde"; ...............
............
quotes[16] = "\"Well, if I called the wrong number, why did you answer
the phone? \" James Thurber";

function rand(n) {
seed = (0x015a4e35 * seed) % 0x7fffffff;
return (seed >> 16) % n;
}
var now = new Date()
var seed = now.getTime() % 0xffffffff
</script>

<script LANGUAGE="JavaScript">
var quoterand = quotes[rand(quotes.length)];
if (quoterand.length < 40) {
document.write(quoterand+"")
} else { document.write(quoterand)
}

</script>


All I od is add another quote, increase the makeArray and it goes
wrong. Any ideas?


TIA
Luke



Hi Luke,

For what you are trying to do, the code used is complete overkill.
Have a look at this.


<html>
<head>
<script type="text/javascript">
var quotes = ["MyFirst Quote" , "Q2" , "Q3", "Q4", "Q5", "Q6", "Q7", "Q8",
"Q9", "Q10", "Q11", "Q12", "Q13", "Q14", "Q15"];

function getRandomQuote(){
numberOfQuotes = quotes.length;
randNum = Math.floor(Math.random()*numberOfQuotes);
return quotes[randNum];
}
</script>

</head>
<body>
Hai, the random quote is:
<br>
<script type="text/javascript">
document.write(getRandomQuote());
</script>

</body>
</html>
 
F

Fred Oz

Erwin Moller wrote:
[...]
function getRandomQuote(){
numberOfQuotes = quotes.length;
randNum = Math.floor(Math.random()*numberOfQuotes);
return quotes[randNum];
}

Very concise, but you could take that a little further:

function getRandomQuote(){
return quotes[Math.floor(Math.random()*quotes.length)];
}
 
E

Erwin Moller

Fred said:
Erwin Moller wrote:
[...]
function getRandomQuote(){
numberOfQuotes = quotes.length;
randNum = Math.floor(Math.random()*numberOfQuotes);
return quotes[randNum];
}

Very concise, but you could take that a little further:

function getRandomQuote(){
return quotes[Math.floor(Math.random()*quotes.length)];
}

Well Fred, :)

You are right of course, but I was trying to keep the steps readable for the
Original Poster.
For educative purposes. :)

Regards,
Erwin Moller
 
L

lharby

Thanks one and all (at Developersdex)

Worked like a charm, and much cleaner code.


:]
Luke
 

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,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top