
November 5th, 2004, 08:13 PM
|
|
Registered User
|
|
Join Date: Oct 2004
Location: Washington State
Posts: 7
Time spent in forums: 50 m 44 sec
Reputation Power: 0
|
|
|
Can I search for a match with an array somehow?
I'm trying to get a list of picture numbers as I use each picture, then generate a random number for the next pic and check it against the array to be sure I don't get duplicate pics. I don't know very much about the match() function because strangely, I can't seem to find very much on the web about it. If there's an easier way to check for a match with all array values, please tell me how, otherwise, can someone explain how to use match properly if it's possible to do what I'm trying to do? Here's the code I have (causes XP SP2 to abort the script after a second because it's too slow. Also note that the disp array is in a separate external .js file that just contains a list of the picture names to use.):
var dispLoc="<td><img src=sigs/disp/";
var i=0;
var columns=7;
var usedName=new Array();
function ranNumberGen(){
var randVal=Math.floor(Math.random()*(disp.length))
dispNo=randVal;
}
function writeRow(){
for (dispRow=0; dispRow<(columns+1); dispRow++){
ranNumberGen();
if (disp[dispNo].match(usedName[i++]) != null){
ranNumberGen();
}else{
if ((usedName.length)==0){
usedName[0]=disp[dispNo];
}else{
usedName[((usedName.length)+1)]=disp[dispNo];
}
document.write(dispLoc + disp[dispNo] +" border=0 alt=\""+ dispRow +". "+disp[dispNo]+"\"></td>");
}
}
}
So basically I want a line of 7 pics, the name chosen from the disp array is randomly selected, and the name is copied into the usedName array. The next pic is chosen at random, and hopefully the name chosen can be checked against the usedName array, if it has already been used, ranNumberGen() can be run again to pick a new index number. When a number is found that doesn't match any already used ones, it writes the next table cell and notes the pic name used in the usedName array, etc, etc. If I cahnge line:
if (disp[dispNo].match(usedName[i++]) != null){
to:
if ((disp[dispNo])==(usedName[i++])){
the page will at least write, but there are still duplicate pics. It does nothing apparently to check the name against the entire usedName array (either that or the lower line to write into the arrayu does nothing. Who knows.)
I'm relatively new to JavaScript at this level, document.write and silly JS clocks are one thing, this is something else. Any help would be appreciated, feel free to add me to AIM (gaston9x19) or MSN (gaston.glock@gmail.com). Thanks very much!
Last edited by gaston9x19 : November 5th, 2004 at 08:20 PM.
Reason: wrong line in FOR loop
|