Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-25-2012, 03:24 AM   PM User | #1
noobatjavascrip
New Coder

 
Join Date: Sep 2012
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
noobatjavascrip is an unknown quantity at this point
Random image generator with no repeats

I have a table filled with 9 images. I have a loop that goes through 9 times and a random number generator inside my loop. I can get all of my images to scramble randomely. The problem is I only want each image to show up one time. I have been at it for a while now and can not figure it out. Even any help would be appreciated or a way to approach the problem.

Thanks

<html>
<head>
<script>
var pictures = new Array (9)
pictures[0] = "tree0_01.jpg"
pictures[1] = "tree0_02.jpg"
pictures[2] = "tree0_03.jpg"
pictures[3] = "tree0_04.jpg"
pictures[4] = "tree0_05.jpg"
pictures[5] = "tree0_06.jpg"
pictures[6] = "tree0_07.jpg"
pictures[7] = "tree0_08.jpg"
pictures[8] = "tree0_09.jpg"




function scramble(){

for (i=0;i<9;i++){
r=Math.floor(Math.random()*8)
document.images[i].src=pictures[r]

}

}
</script>
</head>
<body>
<center><table id="Table_01" width="600" height="600" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>
<img src="tree0_01.jpg" width="200" height="200" ></td>
<td>
<img src="tree0_02.jpg" width="200" height="200" ></td>
<td>
<img src="tree0_03.jpg" width="200" height="200" ></td>
</tr>
<tr>
<td>
<img src="tree0_04.jpg" width="200" height="200" ></td>
<td>
<img src="tree0_05.jpg" width="200" height="200" ></td>
<td>
<img src="tree0_06.jpg" width="200" height="200" ></td>
</tr>
<tr>
<td>
<img src="tree0_07.jpg" width="200" height="200" ></td>
<td>
<img src="tree0_08.jpg" width="200" height="200" ></td>
<td>
<img src="tree0_09.jpg" width="200" height="200" ></td>
</tr>
</table><br>
<input type="button" onclick="scramble()" value="scramble"></center></br>
</body>
</html>
noobatjavascrip is offline   Reply With Quote
Old 10-25-2012, 07:25 AM   PM User | #2
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,553
Thanks: 9
Thanked 480 Times in 463 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Code:
var pictures = new Array (9)
pictures[0] = "tree0_01.jpg"
pictures[1] = "tree0_02.jpg"
pictures[2] = "tree0_03.jpg"
pictures[3] = "tree0_04.jpg"
pictures[4] = "tree0_05.jpg"
pictures[5] = "tree0_06.jpg"
pictures[6] = "tree0_07.jpg"
pictures[7] = "tree0_08.jpg"
pictures[8] = "tree0_09.jpg"

Array.prototype.shuffle=function(){
  function Rnd(w) {return parseInt(Math.random() * (w + 1));}
  var that= ([]).concat(this),mx=that.length, r=[], mx2 = mx-1;
  for(var i=0; i< mx; i++){
		var slot= Rnd(mx2 - i );
		r[i]=that.splice(slot  , 1)[0];
  }
  return r;
}//end shuffle

pictures=pictures.shuffle();


function scramble(){

for (i=0;i<9;i++){
  document.images[i].src=pictures[i];
}	

}
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%

Last edited by rnd me; 10-25-2012 at 07:30 AM..
rnd me is offline   Reply With Quote
Old 10-26-2012, 04:18 AM   PM User | #3
noobatjavascrip
New Coder

 
Join Date: Sep 2012
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
noobatjavascrip is an unknown quantity at this point
I appreciate the quick response

I added the code before the scramble() function, but it did not work.

If you have anymore input that would be great
noobatjavascrip is offline   Reply With Quote
Old 10-26-2012, 04:23 AM   PM User | #4
noobatjavascrip
New Coder

 
Join Date: Sep 2012
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
noobatjavascrip is an unknown quantity at this point
I apologize, it does work. But only one time. I have to refresh the page every time. Is there anyway to have it work everytime I push the scramble button
noobatjavascrip is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:10 AM.


Advertisement
Log in to turn off these ads.