By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 428,786 Members | 2,241 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,786 IT Pros & Developers. It's quick & easy.

# Images array - need a loop

 P: 58 Hi, can anyone give me a hand. I'm currently displaying 4 random images at the top of a page. I did this using an array of 35 pictures and then writing them to page. The problem I have is that in theory you could get the same image 4 times. I quite often get 2 of the same picture come up. What's the easiest way of saying "once an image is assigned to a variable, take it out of the array"? Here's my code so far.. images = new Array(34); images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; images = ""; index = Math.floor(Math.random() * images.length); index2 = Math.floor(Math.random() * images.length); index3 = Math.floor(Math.random() * images.length); index4 = Math.floor(Math.random() * images.length); Jun 25 '07 #1
Share this Question
4 Replies

 Expert 100+ P: 1,351 just perform if statements, if the first equals the second, redo the second, and so on. Jun 25 '07 #2

 Expert Mod 5K+ P: 5,294 hi ... there are several ways for you to avoid the double images ... you may use 4 arrays instead of 1 ... adapt the random number to not to be equal to each other ... and you may adapt the array of course ... have a look at array methods like shift(), unshift(), splice() and pop() ... with that you may manipulate the array to your needs ... but be aware ... some manipulate the original array, others give you back the elements ... kind regards ... Jun 25 '07 #3

 P: 58 Thanks a lot for your replies. I did try and implement a set of if statements and failed miserably! I'll do the set of 4 arrays. That sounded easy enough. Cheers! Jun 25 '07 #4

 Expert Mod 5K+ P: 5,294 hi ;) ... have a look at the following example how you may get 4 distinct random-numbers and refer to them: [HTML] [/HTML] kind regards ... Jun 25 '07 #5

### Post your reply

Sign in to post your reply or Sign up for a free account. 