Oxford Computing Man Oxford Computing Man - 29 days ago 10
HTML Question

Traffic light sequence code not working, don't know why

I am have been trying to get this to work. My class mates cannot see the problem. Any helps to try and get the traffic lights to show when the button is clicked will be much appreciated.
Here is the code I have all Trs come up when summoned.

<!DOCTYPE html>
<html>
<title>Traffic</title>
<body>
<h1>Traffic Lights</h1><p id="jam"></p>
<img id="TR" src="Tr 1.png" style="width:200px"></img>
<br/>
<button onclick="changeLights()">Click here</button>

<script>
var list=["Tr 1.png","Tr 2.png","Tr 3.png","Tr 4.png"];
var image=document.getElementById("TR");
var count=0;


function changeLights() {
if count=0 {
image.src="Tr 1.png";
count=1;
} else if count=1 {
image.src="Tr 2.png";
count=2;
} else if count=2 {
image.src="Tr 3.png";
count=3;
} else > 3 {
image.src="Tr 4.png";
count=0;
}


}
</script>




Answer

The Javascript if block should look like this.

if (count == 0) {
    image.src="Tr 1.png";
        count=1;
} else if (count == 1) {
    image.src="Tr 2.png";
    count=2;
} else if (count == 2) {
   image.src="Tr 3.png";
   count=3;
} else {
    image.src="Tr 4.png";
    count=0;
}
Comments