Hi!
Login or Register new account

We know from lesson #1 that JavaScript can change the style of text, but now
We'll do something more! We will use it as a game! And change the images with it!


Check what's inside a chest!

code:

                                
                                < script >
function changeImage() {
var image = document.getElementById('myImage');
if (image.src.match("closed")) {
var x = Math.floor((Math.random() * 10) + 1);
if(x>5) {
image.src = "images/full.png";
document.getElementById("temporary").innerHTML = "Horraaay! You got the gold! Want to try again? Close it!";
}
if(x<5) {
image.src = "images/empty.png";
document.getElementById("temporary").innerHTML = "Ohhh! No luck this time! Close it and try again!";
}
}
else {
image.src = "images/closed.png";
document.getElementById("temporary").innerHTML = "Lets see what's inside the chest again!";
}
}
< /script >