PDA

View Full Version : Excluding an image from a random list

isaaclloyd
03-23-2005, 08:19 AM
Here is my Code:

<script language="JavaScript" type="text/javascript">
<!--
n=3;// number of all gifs
g=1;//number of random gifs

//no need to modify bellow
var theImages = new Array() //array of gifs
for(var i=0;i<n;i++){
theImages[i]='0'+i+'.gif';
}
//randomize function
function rand(){
r = Math.floor(Math.random()*n);
return r;
}
function showImages(){
var rr = new Array()//array of radom numbers
rr[0]=-1;
for(var j=0;j<g;j++){
rand();
for(var k=0;k<rr.length;k++){//check whether new random number reapets a former one or not
if(k!=j&&r==rr[k]){
var j=j-1;break
}
else{rr[j]=r;}//if not, create new array's element
}
document.getElementById('g'+j).setAttribute('src',theImages[rr[j]]);//set new image
}
}
// -->
</script>

<a href="#" onclick="showImages();return false">
<IMG SRC="00.gif" ID="g0" ALT="Click to View Your Fortune" BORDER="0"></A>

What I am trying to figure out is how to EXCLUDE the origenal image (00.gif) from being one of the random selections. When the main image is clicked I want it to show a picture from a random list. However I don't want the main image to be in that list. Any comments/suggestions are greatly apreciated. Thanks.

~Isaac Lloyd~

Kor
03-23-2005, 09:10 AM
I think you may:

- either build your first array excluding the 0
- or remove the desired element from the final array using splice() method

isaaclloyd
03-23-2005, 10:24 AM
could you give me an example or tell me how I could go about excluding the 0 on the first array, or how to do the splice() thing? Thanks for your time.

~Isaac Lloyd~

hinokata
03-23-2005, 10:43 AM
Been a little while since I've done Javascipt, but I think this might do it for you.

for(var i=0; i<n; i++)
{
theImages[i]='0' + (i + 1) +'.gif';
}

isaaclloyd
03-23-2005, 10:54 AM
instead it showed a "no value" picture. It's like that comment makes that first image an error if it shows up again. I just don't want it to be included or show up again period. Thanks for your ideas though... Anyone else?

~Isaac Lloyd~

hinokata
03-23-2005, 11:17 AM
Sorry...I just don't remember enough about Javascript to give you hand. :(

isaaclloyd
03-23-2005, 11:32 PM
How about you Mr. J. ? Any ideas?

Kor
03-24-2005, 08:07 AM
Try this

...
}
if(theImages[rr[j]=='00.gif'){
theImages[rr[j]=theImages[rr[j+1]];
j++
}
document.getElementById('g'+j).setAttribute('src',theImages[rr[j]]);//set new image
}
}
// -->
</script>

Kor
03-24-2005, 08:12 AM
or:
if(theImages[rr[j]=='00.gif'){
showImages()
}
else{
document.getElementById('g'+j).setAttribute('src',theImages[rr[j]]);//set new image
}
}
}
// -->
</script>

isaaclloyd
03-24-2005, 08:20 AM
*EDIT* Never mind. I realized afterwards that the image will always be 00.gif cause we never changed it yet. The two methods you suggested didn't work. Any ideas? Thanks for your time.

not working for me... What about something like:

....

IF (FORM.INPUT.SRC = "00.gif") { <----- needs real info. That doesn't work.
showImages()
}
ELSE{
document.getElementById('g'+j).setAttribute('src',theImages[rr[j]]);//set new image
}

My Image is: <INPUT TYPE="image" NAME="luckyimage" ID="g0" SRC="00.gif" onclick="checkluckyimg();return false" VALUE="newluck">