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
- */
7
1
const trafficLight = {
8
- possibleStates : [ "green" , "orange" , "red" ] ,
9
- stateIndex : 0 ,
10
- } ;
11
-
2
+ possibleStates : [ "green" , "orange" , "red" ] ,
3
+ stateIndex : 0 , } ;
4
+
12
5
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
- }
22
6
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