JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 5th, 2004, 08:13 PM
gaston9x19 gaston9x19 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Washington State
Posts: 7 gaston9x19 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old November 7th, 2004, 08:29 PM
ProggerPete ProggerPete is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Brisbane, Australia
Posts: 1,442 ProggerPete User rank is Sergeant (500 - 2000 Reputation Level)ProggerPete User rank is Sergeant (500 - 2000 Reputation Level)ProggerPete User rank is Sergeant (500 - 2000 Reputation Level)ProggerPete User rank is Sergeant (500 - 2000 Reputation Level)ProggerPete User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 1 h 37 m 42 sec
Reputation Power: 28
Keep it simple

Code:
function beenUsed(imageNo)
{
for (i=0; i < usedImageNos.length; i++)
{
if (usedImageNos[i] == imageNo) return false;
}
return true;
}
__________________
Like the answers I give? Why not ask me directly at my forum. I'm always glad to help.

Javascript scripts and tips can be found at Dynamic Tools.
Check out DynamicTable, the best javascript table sorter around.
Get reliable and affordable hosting at www.thinksmarthosting.com

Reply With Quote
  #3  
Old November 8th, 2004, 06:50 PM
gaston9x19 gaston9x19 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Washington State
Posts: 7 gaston9x19 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 44 sec
Reputation Power: 0
Quote:
Originally Posted by ProggerPete
Keep it simple

Code:
function beenUsed(imageNo)
{
for (i=0; i < usedImageNos.length; i++)
{
if (usedImageNos[i] == imageNo) return false;
}
return true;
}


Thanks very much man, works great! I made it part of a WHILE loop and I get no repeat images. Awesome!

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Can I search for a match with an array somehow?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap