I have a set of 4 light bulbs:
<div id="bulb1" class="lightbulb"><img src=".\images\bulb.png" /></div>
<div id="bulb2" class="lightbulb"><img src=".\images\bulb.png" /></div>
<div id="bulb3" class="lightbulb"><img src=".\images\bulb.png" /></div>
<div id="bulb4" class="lightbulb"><img src=".\images\bulb.png" /></div>
My task is quite unique - I need to accomplish it using JavaScript.
I have an array with 4 elements {1, 2, 3, 4}. Initially, all bulb images are hidden.
The goal is to randomly illuminate one bulb from the array. For example, if 2 is selected, then the second bulb will light up.
Then after 500 milliseconds, another random selection is made - say 4 this time, illuminating the fourth bulb while hiding the second one.
This process should repeat four times, ensuring that each time a different bulb lights up. What would be the best approach and structure for achieving this?
All bulbs are initially turned off by calling this function:
function hideBulbImages()
{
document.getElementById('bulb1').style.visibility = "hidden";
document.getElementById('bulb2').style.visibility = "hidden";
document.getElementById('bulb3').style.visibility = "hidden";
document.getElementById('bulb4').style.visibility = "hidden";
}
I am considering implementing the showBulbImage
function...
I have created a function to sequentially illuminate the bulbs every second like so:
function showBulbImages()
{
var blink_count = 0;
var blink_the_bulbs = setInterval(function() {
blink_count+=1;
hideBulbImages();
var blinking_bulb = "bulb" + blink_count;
document.getElementById(blinking_bulb).style.visibility = "visible";
if (blink_count > 4)
{
clearInterval(blink_the_bulbs);
}
}, 1000);
}
Now, the challenge lies in randomizing the visibility of the bulbs.