Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e39ae3c

Browse files
committed
Completed traffic light exercise
1 parent 46b1b28 commit e39ae3c

File tree

1 file changed

+14
-30
lines changed

1 file changed

+14
-30
lines changed
Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,17 @@
1-
"use strict";
2-
/**
3-
* The `possibleStates` property define the states (in this case: colours)
4-
* in which the traffic light can be.
5-
* The `stateIndex` property indicates which of the possible states is current.
6-
*/
71
const trafficLight = {
8-
possibleStates: ["green", "orange", "red"],
9-
stateIndex: 0,
10-
};
11-
2+
possibleStates: ["green", "orange", "red"],
3+
stateIndex: 0,};
4+
125
let cycle = 0;
13-
while (cycle < 2) {
14-
const currentState = trafficLight.possibleStates[trafficLight.stateIndex];
15-
console.log("The traffic light is on", currentState);
16-
17-
// TODO
18-
// if the color is green, turn it orange
19-
// if the color is orange, turn it red
20-
// if the color is red, add 1 to cycles and turn it green
21-
}
226

23-
/**
24-
* The output should be:
25-
26-
The traffic light is on green
27-
The traffic light is on orange
28-
The traffic light is on red
29-
The traffic light is on green
30-
The traffic light is on orange
31-
The traffic light is on red
32-
33-
*/
7+
while (cycle < 2) {
8+
const currentState = trafficLight.possibleStates[trafficLight.stateIndex];
9+
console.log("The traffic light is on", currentState);
10+
trafficLight.stateIndex++;
11+
12+
13+
if (trafficLight.stateIndex === 3) {
14+
trafficLight.stateIndex = 0;
15+
cycle++;
16+
}
17+
}

0 commit comments

Comments
 (0)