-
Notifications
You must be signed in to change notification settings - Fork 0
/
traffic-light-2.js
41 lines (37 loc) · 1.04 KB
/
traffic-light-2.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
38
39
40
41
"use strict";
/**
* The `possibleStates` property define the states (in this case: colours)
* in which the traffic light can be.
* The `stateIndex` property indicates which of the possible states is current.
*/
const trafficLight = {
possibleStates: ["green", "orange", "red"],
stateIndex: 0,
};
let cycle = 0;
while (cycle < 2) {
const currentState = trafficLight.possibleStates[trafficLight.stateIndex];
console.log("The traffic light is on", currentState);
// TODO
// if the color is green, turn it orange
if (currentState === "green") {
trafficLight.stateIndex=1;
}
// if the color is orange, turn it red
else if (currentState === "orange"){
trafficLight.stateIndex=2;
}
// if the color is red, add 1 to cycles and turn it green
else if (currentState === "red") {
cycle+=1;
trafficLight.stateIndex=0;
}
}
/**
* The output should be:
The traffic light is on green
The traffic light is on orange
The traffic light is on red
The traffic light is on green
The traffic light is on orange
The traffic light is on red