i have a 6x6 html-table
. Each <td>
holds <img id="1back" src="" />
where the id
of each img
-element is declared as "1back"
to "36back"
. Further I have 18 .jpg files named "1.jpg"
to "18.jpg"
. Each image has to appear in the grid twice , distributed randomly.
I am trying to accomplish this as follows:
function PopulateGrid() {
for (var i = 1; i <= 18;i++) //For each image file
{
var randomnumber = Math.floor(Math.random() * (36 - 1 + 1)) + 1; // get random # 1-36
while (document.getElementById(randomnumber + "back").src != "")
{
//if the randomly chosen <img> id is not "" genrate another random number
randomnumber = Math.floor(Math.random() * (36 - 1 + 1)) + 1;
}
document.getElementById(randomnumber + "back").src = "Images/" + i + ".jpg";
//Doing this twice as every image has to appear twice
while (document.getElementById(randomnumber + "back").src != "") {
randomnumber = Math.floor(Math.random() * (36 - 1 + 1)) + 1;
}
document.getElementById(randomnumber + "back").src = "Images/" + i + ".jpg";
}
}
I am running into wierd behavior in IE, Chrome and FireFox. The script fails in the first while condition. It seems to me like some endless loop. I do not get any error message the page simply never stops loading.
I guess this must be something obvious as usual but I simply can't see the problem here.