-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise1.ts
48 lines (41 loc) · 1.37 KB
/
exercise1.ts
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
38
39
40
41
42
43
44
45
46
47
48
enum TrafficLight {
Red = "Red",
Yellow = "Yellow",
Green = "Green"
}
class TrafficLightRobot {
private currentLight: TrafficLight;
constructor() {
this.currentLight = TrafficLight.Red;
this.updateUI();
}
public start() {
setInterval(() => this.changeLight(), 10000);
}
private changeLight() {
switch (this.currentLight) {
case TrafficLight.Red:
this.currentLight = TrafficLight.Green;
break;
case TrafficLight.Yellow:
this.currentLight = TrafficLight.Red;
break;
case TrafficLight.Green:
this.currentLight = TrafficLight.Yellow;
break;
}
this.updateUI();
}
private updateUI() {
const redLight = document.getElementById("red");
const yellowLight = document.getElementById("yellow");
const greenLight = document.getElementById("green");
if (redLight && yellowLight && greenLight) {
redLight.style.opacity = this.currentLight === TrafficLight.Red ? '1' : '0.3';
yellowLight.style.opacity = this.currentLight === TrafficLight.Yellow ? '1' : '0.3';
greenLight.style.opacity = this.currentLight === TrafficLight.Green ? '1' : '0.3';
}
}
}
const robot = new TrafficLightRobot();
robot.start();