|
6 | 6 | */
|
7 | 7 | const trafficLight = {
|
8 | 8 | possibleStates: ["green", "orange", "red"],
|
9 |
| - stateIndex: 0, |
| 9 | + stateIndex: 0, |
10 | 10 | };
|
11 |
| - |
| 11 | +localStorage |
12 | 12 | let cycle = 0;
|
13 |
| -while (cycle < 2) { |
| 13 | + |
| 14 | +while (cycle < 2) { |
14 | 15 | const currentState = trafficLight.possibleStates[trafficLight.stateIndex];
|
15 | 16 | console.log("The traffic light is on", currentState);
|
16 | 17 |
|
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 | + } |
21 | 27 | }
|
22 | 28 |
|
23 | 29 | /**
|
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