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:
01.
<b>
02.
< script ><br>
03.
function
changeImage() {<br>
04.
var
image = document.getElementById(
'myImage'
);<br>
05.
if
(image.src.match(
"closed"
)) {<br>
06.
var
x = Math.floor((Math.random() * 10) + 1);<br>
07.
if
(x>5) {<br>
08.
image.src =
"images/full.png"
;<br>
09.
document.getElementById(
"temporary"
).innerHTML =
"Horraaay! You got the gold! Want to try again? Close it!"
;<br>
10.
}<br>
11.
if
(x<5) {<br>
12.
image.src =
"images/empty.png"
;<br>
13.
document.getElementById(
"temporary"
).innerHTML =
"Ohhh! No luck this time! Close it and try again!"
;<br>
14.
}<br>
15.
}<br>
16.
else
{<br>
17.
image.src =
"images/closed.png"
;<br>
18.
document.getElementById(
"temporary"
).innerHTML =
"Lets see what's inside the chest again!"
;<br>
19.
}<br>
20.
}<br>
21.
< /script ><br>
22.
</b>