472,782 Members | 3,711 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 software developers and data experts.

Images array - need a loop

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 2900
1,351 Expert 1GB
just perform if statements, if the first equals the second, redo the second, and so on.
Jun 25 '07 #2
5,390 Expert Mod 4TB
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
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
5,390 Expert Mod 4TB
hi ;) ... have a look at the following example how you may get 4 distinct random-numbers and refer to them:

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;

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

for (var i in indices) {
alert(j + '/' + i);

kind regards ...
Jun 25 '07 #5

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

Similar topics

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...
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?
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...
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....
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...
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"
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...
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...
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...
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: lllomh | last post by:
How does React native implement an English player?

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.