File tree Expand file tree Collapse file tree 2 files changed +47
-7
lines changed
Week1/prep-exercises/1-traffic-light Expand file tree Collapse file tree 2 files changed +47
-7
lines changed Original file line number Diff line number Diff line change 4
4
* that moment.
5
5
*/
6
6
const trafficLight = {
7
- state : "green" ,
8
- } ;
7
+ state : "green" } ;
9
8
10
9
let rotations = 0 ;
11
- while ( rotations < 2 ) {
12
- const currentState = trafficLight . state ;
10
+ while ( rotations < 2 ) {
11
+ let currentState = trafficLight . state ;
13
12
console . log ( "The traffic light is on" , currentState ) ;
14
13
14
+
15
+
15
16
// TODO
16
17
// if the color is green, turn it orange
17
18
// if the color is orange, turn it red
18
19
// if the color is red, add 1 to rotations and turn it green
19
- }
20
+
21
+ switch ( currentState ) {
22
+
23
+ case "green" :
24
+ trafficLight . state = "orange" ;
25
+ break ;
26
+
27
+ case "orange" :
28
+ trafficLight . state = "red" ;
29
+ break ;
30
+
31
+ case "red" :
32
+ trafficLight . state = "green" ;
33
+ rotations ++ ;
34
+
35
+
36
+ } }
37
+
38
+
20
39
21
40
/**
22
41
* The output should be:
@@ -28,4 +47,4 @@ The traffic light is on green
28
47
The traffic light is on orange
29
48
The traffic light is on red
30
49
31
- */
50
+ */
Original file line number Diff line number Diff line change @@ -14,11 +14,32 @@ while (cycle < 2) {
14
14
const currentState = trafficLight . possibleStates [ trafficLight . stateIndex ] ;
15
15
console . log ( "The traffic light is on" , currentState ) ;
16
16
17
+ switch ( currentState ) {
18
+
19
+ case "green" :
20
+ trafficLight . stateIndex ++ ;
21
+ break ;
22
+
23
+ case "orange" :
24
+ trafficLight . stateIndex ++ ;
25
+ break ;
26
+
27
+ case "red" :
28
+ trafficLight . stateIndex = 0 ;
29
+ cycle ++ ;
30
+
31
+ }
32
+
33
+
34
+ }
35
+
36
+
37
+
17
38
// TODO
18
39
// if the color is green, turn it orange
19
40
// if the color is orange, turn it red
20
41
// if the color is red, add 1 to cycles and turn it green
21
- }
42
+
22
43
23
44
/**
24
45
* The output should be:
You can’t perform that action at this time.
0 commit comments