...

View Full Version : random



spenoir
08-10-2006, 11:18 AM
Hi Everyone,
Its a glum morning here in london, think the summer is on its way out already, time to hibernate I guess. :)
Anyway, can anyone tell me whats wrong with the code below, i'm using the following code to output:



<script language="JavaScript">document.write('article[choice]'));</script>




var range = 6;
// Number of alternative nodes
var article = new Array(selectnode);
// Array to hold nodes
article[0] = "Content1";
article[1] = "Content2";
article[2] = "Content3";
article[3] = "Content4";
article[4] = "Content5";
article[5] = "Content6";

function pickRandom(range) {
if (Math.random) return Math.round(Math.random() * (range-1));
else { var now = new Date(); return (now.getTime() / 1000) % range; }
}

var choice = pickRandom(selectnode);


thanks in advance,

jpalfree
08-10-2006, 07:18 PM
hmm... well, all i see that is (perhaps) wrong is your declaration of
var range = 6;
should that be var selectnode = 6; ?
and you have too many brackets on your document.write()

that's all i see.

Pyth007
08-10-2006, 08:30 PM
I can't figure out what your overall goal is....
Are you trying to load a random "content" from an array? From what I can tell, aside from the extra paran's that jpalfree points out, your document.write command will only output: article[choice]
You'd need to remove it from the quotes to get it to display what is stored in the array.

Mr J
08-10-2006, 08:34 PM
And article[choice] in the document.write does not want to be in quotes, also use Math.floor() in the random statement.

Something like this



<script type="text/javascript">

var article = new Array();
article[0] = "Content1";
article[1] = "Content2";
article[2] = "Content3";
article[3] = "Content4";
article[4] = "Content5";
article[5] = "Content6";

range=article.length

function pickRandom() {
if(Math.random){
return Math.floor(Math.random() * range)
}
else{
var now = new Date();
return (now.getTime() / 1000) % range;
}
}

</script>

<script type="text/javascript">
document.write(article[pickRandom()]);
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum