I've been working on coding a button that can change the background of my webpage to display a random image. Initially, my code seemed functional, but it was only displaying the last image listed in my series of if/else statements. I suspect that using switch cases might be more suitable. My goal is for the code to select a random image by using Math.floor(Math.random()*3)
. Currently, I have set up an if statement as follows:
var randomImg = Math.floor(Math.random()*3);
if (randomImg === 1) {
$('html').css("background-image","url(http://i1-news.softpedia-static.com/images/news-700/Bomb-Threat-Prompts-Evacuation-of-Microsoft-s-Greek-Headquarters.png?1363255993)")
}
if (randomImg === 3) {
$('html').css("background","url(http://www.zdeai.com/news/wp-content/uploads/sites/5/2013/10/d6802__tour-googles-luxurious-googleplex-campus-in-california.jpg)")
}
if (randomImg === 2) {
$('html').css("background","url(http://upload.wikimedia.org/wikipedia/commons/8/84/Apple_Campus_One_Infinite_Loop_Sign.jpg)")
}
Do you think utilizing a switch statement would be better for this scenario? Or perhaps incorporating a for loop could enhance the functionality?
Thank you for your assistance and input.