-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
37 lines (30 loc) · 912 Bytes
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"use strict";
const trafficlight = {
possibleStates : [ "green", "orange", "red"],
stateIndex :0,
};
let cycle = 0;
function updateTrafficLight() {
const currentState = trafficlight.possibleStates[trafficlight.stateIndex];
const trafficLightElement = document.getElementById("traffic-light");
trafficLightElement.textContent = `The traffic light is on ${currentState}`;
trafficLightElement.className = "";
trafficLightElement.classList.add(currentState);
if (currentState === "green") {
trafficlight.stateIndex = 1;
}
else if(currentState === "orange") {
trafficlight.stateIndex = 2;
}
else if(currentState === "red") {
trafficlight.stateIndex = 0;
cycle++;
}
if (cycle < 3) {
setTimeout(updateTrafficLight, 1000);
}
else {
trafficLightElement.textContent = "Traffic light simulation compeleted." ;
}
}
updateTrafficLight();