...

View Full Version : How do I assign the contents of an array to variables?



ilmus
01-27-2005, 08:06 PM
I'm trying to assign the contents of an array to variables as follows:

var ay =picsnum[0];
var by =picsnum[1];
var cy =picsnum[2];

all three variables should return an integer which i'm then using to load an image whose location is stored in another array mycontent as follows:

document.write('<img src="' +mycontent[ay]+'" width="135" height="100" align="top">')

I know the second part is working cause i'm getting placeholders for the pictures in my browser, but the location of the pictures is undefined.

Can somebody help me please?

Thanks a lot
ilmus

requestcode
01-27-2005, 08:13 PM
Are you sure that the images are loaded in the array?

ilmus
01-27-2005, 08:22 PM
Not the image itself...the path where it is stored I meant:

mycontent[1]='images/pics/XAR1024A-400.jpg'

ilmus
01-27-2005, 09:41 PM
i'm still trying to figure this out. i've tried the pop() method but to no avail.

Any help pls?

glenngv
01-28-2005, 03:57 AM
Can you post all the related codes?

ilmus
01-28-2005, 02:08 PM
Ok...here comes the related code:

<script language="JavaScript">
var mycontent=new Array()
//specify random content below.
mycontent[0]='pic1.jpg'
mycontent[1]='pic2.jpg'
mycontent[2]='pic3.jpg'
mycontent[3]='pic4.jpg'
mycontent[4]='pic5.jpg'
mycontent[5]='pic6.jpg'
mycontent[6]='pic7.jpg'
mycontent[7]='pic8.jpg'



var picnums = new array();
//array with 3 different numbers
var foundunused = false;
for (x=0; x<3; x++){
while (!foundunused ) {
picnums[x]=Math.floor(Math.random()*mycontent.length);
foundunused = true;
for (z=0; z<x; z++){
if (picnums[x] == picnums[z]) {
foundunused = false;
}
}
}
}

var ay =picsnum[0];
var by =picsnum[1];
var cy =picsnum[2];
</script>

</head>

<body>


<table width="100%" border="0" cellpadding="1" cellspacing="1">
<tr>
<td colspan="3" bgcolor="#0A960A">
<div align="center"><font color="#FFFFFF" face="Verdana, Arial, Helvetica, sans-serif"><strong>Xaghra</strong></font></div></td>
</tr>
<tr>
<td width="135" height="100" align="left" valign="top">
<div align="left">
<script language="JavaScript">
<!--

document.write('<img src=images/pics/tamenzja/' +mycontent[ay]+' width="135" height="100" align="top">')

//-->
</script>
</div></td>


Thanks

ilmus
01-28-2005, 06:03 PM
Problem solved...I used the following code instead of the picnums array and got the desired results:

var ay, by, cy
do {
ay=Math.floor(Math.random()*mycontent.length);
by=Math.floor(Math.random()*mycontent.length);
cy=Math.floor(Math.random()*mycontent.length); }
while ((ay == by) | (ay == cy) | (by == cy)) ;

Thanks for your help.

Regards,
ilmus :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum