So, I've got my code ready to generate and print a random number between "-20 and 20".
What I'd like to do, and this is where I'm stuck, is to assign 4 different images to a number range between my total range of "-20 to 20".
For instance:
image1.gif
would be assigned to any random # between "-20 and -10"image2.gif
assigned to "-9 and 0"image3.gif
to "1 and 10"image4.gif
to "11 and 20"
Any suggestions much welcomed!
EDIT: So based on Jon's answer, i've changed the last two lines to a document.write and am trying to get my actual images shown above the text output. I'm however aware of a faulty line in the IMG.SRC. What would this be needed to change to if my images were lying in my root dir? Thanks in advance.
<html>
<title>RANDOM IMAGE TO NUMBER RANGE</title>
<body>
<center>
<script language="javascript" type="text/javascript">
var ranges = [
{ from: 11, image: "image4.jpg" },
{ from: 1, image: "image3.jpg" },
{ from: -9, image: "image2.jpg" },
{ from: -20, image: "image1.jpg" }
];
var random_num = (Math.round((Math.random()*41)+-20));
var image = "default";
for(var i = 0; i < ranges.length; ++i) {
if(random_num >= ranges[i].from) {
image = ranges[i].image;
break;
}
}
document.write("Number: " + random_num + " parts, " + " Imagetitle: = " + image);
document.write('<img src="'+ranges[image]+'">');
</script>
<br></br>
<div>
CLICK <a href="javascript:history.go(0);">REFRESH or F5</a> FOR A RANDOM IMAGE on NUMBER-RANGE.
</div>
</center>
</body>
</html>