random reading object array

wxdqz

New Member
Heya all

I've been trying to write a script that will place a set of customer details on a page in random order (althought not really random). I've been able to generate the random number in the range I need, but I'm having a real problem using that number to pull the relevant object out of an array of objects.

OK, what the h*ll is he on about, I hear you cry. lets have a look at the code:

//This sets up the object methods
function showPic() { document.write(this.pic); }
function showText() { document.write(this.text); }
function showLink() { document.write(this.link); }
function showTitle(){ document.write(this.title); }

//This creates the object AdObject
function AdObject(pic, text, link, title)
{ this.pic = pic;
this.text = text;
this.link = link;
this.title = title;

this.showPic = showPic;
this.showText = showText;
this.showLink = showLink;
this.showTitle = showTitle; }

//This creates the array Ads with 3 different AdObjects
Ads = new Array();
Ads[0] = new AdObject("pic0","text0","link0","title0");
Ads[1] = new AdObject("pic1","text1","link1","title1");
Ads[2] = new AdObject("pic2","text2","link2","title2");

Right, so we've got an array of three objects. Now I find a random number:

var p = (Ads.length)
var preBuffer = new Array()

for (i = 0; i < p; i++){
preBuffer = Ads
}

var Adp = Math.round(Math.random()*(p-1));

Now using the random number Adp, I try to extract an initial object from the array and then figure out where the random number started and extract the other objects in order:

for (a = Adp; a < 4; a++)
{ document.Write(Ads[a].showPic() +" - "+ Ads[a].showText() +" - "+ Ads[a].showLink() +" - "+ Ads[a].showTitle()+"<br>") }
a = a-4

while (a!=Adp)
{ document.Write(Ads[a].showPic() +" * "+ Ads[a].showText() +" * "+ Ads[a].showLink() +" * "+ Ads[a].showTitle()+"<br>")
a++ }

This is pretty dodgy scripting (as I'm not amazingly good at Maths) but I thought the logic was sound.

So my 2 problems.

When I run the page, I manage to extract the values of the first object, but then it only extracts until the end of the array, ie if Adp is 2 it extracts the values of Ads[1] and Ads[2], but then stops (when it should go on to extract Ads[0])

Also the display seems a bit weird. It writes the values like so:

pic1text1link1title1

without any of the spacer things I put in.

It does an even weirder thing if I try and place the script as server-side JScript, but that's another thread's worth!

So - anybody got any ideas what is going on here? Being so close to the code, all I can see is my own logic shining through and can't see what might be the problem.

Sorry for such a HUGE post! :rolleyes:

Cheers

Azz
 
Back
Top