By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,773 Members | 1,330 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,773 IT Pros & Developers. It's quick & easy.

Problem with Random Quote Generator

P: n/a
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[i] = 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
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
lharby wrote:
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[i] = 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>
Jul 23 '05 #2

P: n/a
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)];
}
--
Fred
Jul 23 '05 #3

P: n/a
Fred Oz wrote:
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
Jul 23 '05 #4

P: n/a
Thanks one and all (at Developersdex)

Worked like a charm, and much cleaner code.
:]
Luke
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.