I have the following function:
var randomImages = ["bgr1","bgr2","bgr3","bgr4","bgr5","bgr6"];
var rndNum = Math.floor(Math.random() * randomImages.length);
$(".myDiv").css({ background: "url(/images/" + randomImages[rndNum] + ".gif)" });
Since all the images have the same name except the last numeric value, can’t I simplify this?
This may seem obvious but if you know how many images you have you could replace randomImages.length with a constant e.g. 5 for arguments sake, then you dont need an array so it would be just one line:
Note: math.ceil would be better as then you dont need a +1.