Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with making code more ‘efficient’ 1

Status
Not open for further replies.

mrcrusoe

Programmer
Nov 10, 2006
5
GB
I have created the following code that selects 10 random images from a selection of 59 without any duplicates.
The code works great but as a bit of a newbie to javascript, I’m sure the section where I am checking for duplicates could be made more efficient.
I was wondering if anyone could point me in the right direction in making the ‘check for duplicates’ section more ‘professional’.

Any help would be greatly appreciated, thanks.
Heres the code:
Code:
addLoadListener(function() {
	// set up array with numbers for each photo					 
	imagenumber = new Array(58)
	
	// create array for each image number
	for (i = 0; i < 59; i++) {
		imagenumber[i] = i;
	}
	
	// create new arrays for amount of images to be used on page
	n = new Array(9)
	train = new Array(9)
	trainlink = new Array(9)
	
	// create image and link for each number
	for (i = 1; i < 11; i++) {
		
		train[i] = 'train' + i;
		trainlink[i] = 'trainlink' + i;
		
		n[i] = Math.floor(Math.random() * imagenumber.length);
		
		// check for duplicates
		while (n[2] == n[1]) {
			n[2] = Math.floor(Math.random() * imagenumber.length);
		}
		while (n[3] == n[1] || n[3] == n[2]) {
			n[3] = Math.floor(Math.random() * imagenumber.length);
		}
		while (n[4] == n[1] || n[4] == n[2] || n[4] == n[3]) {
			n[4] = Math.floor(Math.random() * imagenumber.length);
		}
		while (n[5] == n[1] || n[5] == n[2] || n[5] == n[3] || n[5] == n[4]) {
			n[5] = Math.floor(Math.random() * imagenumber.length);
		}
		while (n[6] == n[1] || n[6] == n[2] || n[6] == n[3] || n[6] == n[4] || n[6] == n[5]) {
			n[6] = Math.floor(Math.random() * imagenumber.length);
		}
		while (n[7] == n[1] || n[7] == n[2] || n[7] == n[3] || n[7] == n[4] || n[7] == n[5] || n[7] == n[6]) {
			n[7] = Math.floor(Math.random() * imagenumber.length);
		}
		while (n[8] == n[1] || n[8] == n[2] || n[8] == n[3] || n[8] == n[4] || n[8] == n[5] || n[8] == n[6] || n[8] == n[7]) {
			n[8] = Math.floor(Math.random() * imagenumber.length);
		}
		while (n[9] == n[1] || n[9] == n[2] || n[9] == n[3] || n[9] == n[4] || n[9] == n[5] || n[9] == n[6] || n[9] == n[7] || n[9] == n[8]) {
			n[9] = Math.floor(Math.random() * imagenumber.length);
		}
		while (n[10] == n[1] || n[10] == n[2] || n[10] == n[3] || n[10] == n[4] || n[10] == n[5] || n[10] == n[6] || n[10] == n[7] || n[10] == n[8] || n[10] == n[9]) {
			n[10] = Math.floor(Math.random() * imagenumber.length);
		}
		
		// place the images on the page		
		train[i] = document.getElementById(train[i]);
		train[i].src = 'images/jpg/mainimages/rr_image_' + n[i] + '.jpg';
		trainlink[i] = document.getElementById(trainlink[i]);
		trainlink[i].href = 'images/jpg/mainimages/rr_image_' + n[i] + '_bp.jpg';	
	}
});
 
Hi

Let us change the approach. The following code snippet fills the hmm [tt]Array[/tt] with random numbers without duplicates.
Code:
[b]var[/b] nr=10
[b]var[/b] max=59
[b]var[/b] hmm=[b]new[/b] Array(nr)
[b]for[/b] ([b]var[/b] i=0;i<nr;i++) {
  [b]do[/b] {
    hmm[i]=Math.floor(Math.random()*max)
    [b]var[/b] ok=[b]true[/b]
    [b]for[/b] ([b]var[/b] j=0;j<i;j++) [b]if[/b] (hmm[j]==hmm[i]) ok=[b]false[/b]
  } [b]while[/b] (!ok)
}
Try to change your code yourself based on this code sample. If you have problems, ask.

Feherke.
 
Thanks feherke for your quick and useful response!
I have managed to use the code you provided to find the more 'professional' solution that I was looking for.
Here's my final bit of code for anyone else who may find it useful:
Code:
addLoadListener(function() {
						 
	var n = 10; // number of photos to be used on page
	var photos = 59; // number of photos to choose from
	rand = new Array(n)
	train = new Array(n)
	trainlink = new Array(n)
	
	for (i = 0; i < n; i++) {
  		
		do {
    		rand[i] = Math.floor(Math.random() * photos);
    		var ok = true;
    		for (j = 0; j < i; j++) {
				if (rand[j] == rand[i]) {
					ok = false;
				}
			}
  		} while (!ok)
		
		train[i] = 'train' + (i + 1);
		trainlink[i] = 'trainlink' + (i + 1);
		
		train[i] = document.getElementById(train[i]);
		train[i].src = 'images/jpg/mainimages/rr_image_' + rand[i] + '.jpg';
		trainlink[i] = document.getElementById(trainlink[i]);
		trainlink[i].href = 'images/jpg/mainimages/rr_image_' + rand[i] + '_bp.jpg';	
	}	
});
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top