Click to See Complete Forum and Search --> : Random quote script


jake_604
08-22-2003, 08:45 AM
I have a script that displays random quotes:


<SCRIPT LANGUAGE="JavaScript">
<!--//

var quotnum = "3";
var quote = new Array(1000);

quote[0] = "Quote #1";
quote[1] = "Quote #2";
quote[2] = "Quote #3";

var arandomn=Math.random() * quotnum;
arandomn=Math.round(arandomn);
var daquote =quote[arandomn];
document.write(""+daquote+"");

//-->
</SCRIPT>

How can I edit that script to make it so that if Quote 1 is shown, when the page is refreshed, it will be either Quote 2 or 3. And when Quote 2 is displayed, when the page is refreshed either Quote 1 or 3 will be shown?

I hope someone can help me.

AdamBrill
08-22-2003, 08:55 AM
I don't think that is possible. Since you won't know what the one on the last page was, there is no way to make sure that it isn't that one....

BTW, this is a bad idea:

var quote = new Array(1000);

You should just use this:

var quote = new Array();

setting it to 1000 is just sucking up extra space...

pyro
08-22-2003, 09:19 AM
You might also want to switch

var arandomn=Math.random() * quotnum;

to

var arandomn=Math.random() * quote.length;

and remove the var quotenum = "3"; from the top of your script. This way, as you add more quotes, it will automatically read them.

AdamBrill
08-22-2003, 09:40 AM
You might want to just use this instead:<script type="text/javascript">
<!--//
var quote = new Array("Quote #1",
"Quote #2",
"Quote #3");
arandomn=Math.round(Math.random() * (quote.length-1));
document.write(quote[arandomn]);
//-->
</script>You can add more into the array and it will automatically use them(like Pyro suggested).

Charles
08-22-2003, 09:59 AM
Or you could use...

<script type="text/javascript">
<!--
Array.prototype.random = function () {return this[Math.floor(Math.random() * this.length)]}

document.write(['Quote #1', 'Quote #2', 'Quote #3'].random())
// -->
</script>