Skip to content

Commit 1ad2057

Browse files
Merge pull request #2 from amaniomaisy90/traffic-light-1-prep-exercise
Update traffic-light-2.js
2 parents 168bcb3 + 6241895 commit 1ad2057

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

Week1/prep-exercises/1-traffic-light/traffic-light-2.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,32 @@
66
*/
77
const trafficLight = {
88
possibleStates: ["green", "orange", "red"],
9-
stateIndex: 0,
9+
stateIndex: 0,
1010
};
11-
11+
localStorage
1212
let cycle = 0;
13-
while (cycle < 2) {
13+
14+
while (cycle < 2) {
1415
const currentState = trafficLight.possibleStates[trafficLight.stateIndex];
1516
console.log("The traffic light is on", currentState);
1617

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
18+
19+
if (trafficLight.stateIndex === 0) {
20+
trafficLight.stateIndex = 1;
21+
} else if (trafficLight.stateIndex === 1) {
22+
trafficLight.stateIndex = 2;
23+
} else if (trafficLight.stateIndex === 2) {
24+
cycle++;
25+
trafficLight.stateIndex = 0;
26+
}
2127
}
2228

2329
/**
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-
*/
30+
* The expected output:
31+
* The traffic light is on green
32+
* The traffic light is on orange
33+
* The traffic light is on red
34+
* The traffic light is on green
35+
* The traffic light is on orange
36+
* The traffic light is on red
37+
*/

0 commit comments

Comments
 (0)