473,513 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Images array - need a loop

toxicpaint
58 New Member
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[0] = "<img src='images/1.jpg' />";
images[1] = "<img src='images/2.jpg' />";
images[2] = "<img src='images/3.jpg' />";
images[3] = "<img src='images/4.jpg' />";
images[4] = "<img src='images/5.jpg' />";
images[5] = "<img src='images/6.jpg' />";
images[6] = "<img src='images/7.jpg' />";
images[7] = "<img src='images/8.jpg' />";
images[8] = "<img src='images/9.jpg' />";
images[9] = "<img src='images/10.jpg' />";
images[10] = "<img src='images/11.jpg' />";
images[11] = "<img src='images/12.jpg' />";
images[12] = "<img src='images/13.jpg' />";
images[13] = "<img src='images/14.jpg' />";
images[14] = "<img src='images/15.jpg' />";
images[15] = "<img src='images/16.jpg' />";
images[16] = "<img src='images/17.jpg' />";
images[17] = "<img src='images/18.jpg' />";
images[18] = "<img src='images/19.jpg' />";
images[19] = "<img src='images/20.jpg' />";
images[20] = "<img src='images/21.jpg' />";
images[21] = "<img src='images/22.jpg' />";
images[22] = "<img src='images/23.jpg' />";
images[23] = "<img src='images/24.jpg' />";
images[24] = "<img src='images/25.jpg' />";
images[25] = "<img src='images/26.jpg' />";
images[26] = "<img src='images/27.jpg' />";
images[27] = "<img src='images/28.jpg' />";
images[28] = "<img src='images/29.jpg' />";
images[29] = "<img src='images/30.jpg' />";
images[30] = "<img src='images/31.jpg' />";
images[31] = "<img src='images/32.jpg' />";
images[32] = "<img src='images/33.jpg' />";
images[33] = "<img src='images/34.jpg' />";
images[34] = "<img src='images/35.jpg' />";

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
4 2950
epots9
1,351 Recognized Expert Top Contributor
just perform if statements, if the first equals the second, redo the second, and so on.
Jun 25 '07 #2
gits
5,390 Recognized Expert Moderator Expert
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
toxicpaint
58 New Member
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
gits
5,390 Recognized Expert Moderator Expert
hi ;) ... have a look at the following example how you may get 4 distinct random-numbers and refer to them:

[HTML]
<script>
function generate_four_random_numbers(number, max) {
var indices = {};

for (var i = 0; i < number; i++) {
var index = Math.floor(Math.random() * max);

while (typeof indices[index] != 'undefined') {
index = Math.floor(Math.random() * max);
}

indices[index] = 1;
}

return indices;
}
</script>


<body onload="
var indices = generate_four_random_numbers(4, 34);
var j = 0;

for (var i in indices) {
alert(j + '/' + i);
j++;
};
">
</body>
[/HTML]

kind regards ...
Jun 25 '07 #5

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

Similar topics

2
3367
by: Albert Spencil | last post by:
I have tried several preload scripts found here; plus, some of my own. The only thing that works is the unsophisticated loading of those tiny images. The download consist of 100+ images amounting...
9
12376
by: Michael Burtenshaw | last post by:
I would like to make a slide show using random images. The problem is my host is 250.com, and they don't support cgi-programs. Is there another way to accomplish random images?
3
12260
by: src_mag | last post by:
Hello, I'd like to write JavaScript code that refreshes a frame once a second loading a different image each time. Basically, here's what the script would do: 0 sec load img0.gif 1 sec load...
2
1893
by: Trent | last post by:
Hey All, I am new to javascript and currently coding a site with scripts that are beyond my level of understanding. The problematic page has thumbnail images that can be clicked on to zoom in....
7
1515
by: guitarromantic | last post by:
Hey all. I'm building a news system. I have a page to display all posts made to category=$id, simple enough, now I wanna make an index page that looks up all the categories from the news_topics...
2
2393
by: James Lang | last post by:
Hi I am trying to display in an aspx page an image from the employees table in the sql server 2000 Northhwind database "Select photo from employees where employeeid = 1"
4
3271
by: Aquosus | last post by:
I would like to know how to load images from a file using JavaScript. path = = root/images/kitchens/web all files are jpeg I would like to be able to loop through the file and capture all...
1
1760
by: Tarik Monem | last post by:
Hi guys, As usual, I've found some code from the net, changed it around, added some things and it works, but not exactly the way that I'd like it to work. I want it to work like the app on...
5
13305
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
7161
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5089
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.