I'm a student having trouble with my HTML code. My task is to create a traffic light sequence using the array in my code, which advances the traffic light sequence after each click of the button. It does this by manipulating a single image on the web page.
However, after pressing the button, it only changes the lights to their 'RedAmberLight'
state. The code has been pasted below and any help would be much appreciated:
<!DOCTYPE html>
<html>
<body>
<h1>Changing Traffic Lights w/ Arrays</h1>
<img id="trafficLight" src="RedLight.jpg">
<button type="button" onclick="lightChange()">Change Traffic Lights</button>
<script>
var fileArray = ["RedLight.jpg",
"RedAmberLight.jpg",
"GreenLight.jpg",
"AmberLight.jpg"];
function lightChange() {
var lightColour = trafficLight.src
if (lightColour = fileArray[0]) {
lightColour = fileArray[1];
} else if (lightColour = fileArray[1]) {
lightColour = fileArray[2];
} else if (lightColour = fileArray[2]) {
lightColour = fileArray[3];
} else {
lightColour = fileArray[0];
}
var light = document.getElementById('trafficLight');
light.src = lightColour
}
</script>
</body>
</html>