File tree Expand file tree Collapse file tree 1 file changed +17
-2
lines changed
Week2/prep-exercises/1-traffic-light Expand file tree Collapse file tree 1 file changed +17
-2
lines changed Original file line number Diff line number Diff line change @@ -9,21 +9,36 @@ function getCurrentState(trafficLight) {
9
9
// TODO
10
10
// Should return the current state (i.e. colour) of the `trafficLight`
11
11
// object passed as a parameter.
12
+ return trafficLight . possibleStates [ trafficLight . stateIndex ] ;
13
+
12
14
}
13
-
14
15
function getNextStateIndex ( trafficLight ) {
15
16
// TODO
16
17
// Return the index of the next state of the `trafficLight` such that:
17
18
// - if the color is green, it will turn to orange
18
19
// - if the color is orange, it will turn to red
19
20
// - if the color is red, it will turn to green
20
- }
21
+
21
22
22
23
// This function loops for the number of seconds specified by the `secs`
23
24
// parameter and then returns.
24
25
// IMPORTANT: This is not the recommended way to implement 'waiting' in
25
26
// JavaScript. You will learn better ways of doing this when you learn about
26
27
// asynchronous code.
28
+ const currentState = trafficLight . stateIndex ;
29
+
30
+ if ( currentState === 0 ) {
31
+ return 1 ;
32
+ }
33
+ else if ( currentState === 1 ) {
34
+ return 2 ;
35
+ }
36
+ else if ( currentState === 2 ) {
37
+ return 0 ;
38
+ } }
39
+
40
+
41
+
27
42
function waitSync ( secs ) {
28
43
const start = Date . now ( ) ;
29
44
while ( Date . now ( ) - start < secs * 1000 ) {
You can’t perform that action at this time.
0 commit comments