|
1 | | -"use strict"; |
| 1 | +'use strict'; |
2 | 2 | /** |
3 | | - * Our state variable says what the traffic light's color is at that moment |
| 3 | + * The `trafficLight` object is now no longer a global variable. Instead, |
| 4 | + * it is defined in function `main()` and passed as a parameter to other |
| 5 | + * functions, as and when needed. |
4 | 6 | */ |
5 | 7 |
|
6 | | -function getCurrentTrafficLightState(trafficLight) { |
| 8 | +function getCurrentState(trafficLight) { |
7 | 9 | // TODO |
8 | | - // Should return `green`, `orange` or `red` depending on the state of the given parameter |
| 10 | + // Should return the current state (i.e. colour) of the `trafficLight` |
| 11 | + // object passed as a parameter. |
9 | 12 | } |
10 | 13 |
|
11 | 14 | function getNextStateIndex(trafficLight) { |
12 | 15 | // TODO |
13 | | - // if the color is green, turn it orange |
14 | | - // if the color is orange, turn it red |
15 | | - // if the color is red, turn it green |
| 16 | + // Return the index of the next state of the `trafficLight` such that: |
| 17 | + // - if the color is green, it will turn to orange |
| 18 | + // - if the color is orange, it will turn to red |
| 19 | + // - if the color is red, it will turn to green |
| 20 | +} |
| 21 | + |
| 22 | +// This function loops for the number of seconds specified by the `secs` |
| 23 | +// parameter and then returns. |
| 24 | +// IMPORTANT: This is not the recommended way to implement 'waiting' in |
| 25 | +// JavaScript. You will learn better ways of doing this when you learn about |
| 26 | +// asynchronous code. |
| 27 | +function waitSync(secs) { |
| 28 | + const start = Date.now(); |
| 29 | + while (Date.now() - start < secs * 1000) { |
| 30 | + // nothing do to here |
| 31 | + } |
16 | 32 | } |
17 | 33 |
|
18 | 34 | function main() { |
19 | 35 | const trafficLight = { |
20 | | - possibleStates: ["green", "orange", "red"], |
21 | | - currentStateIndex: 0, |
| 36 | + possibleStates: ['green', 'orange', 'red'], |
| 37 | + stateIndex: 0, |
22 | 38 | }; |
23 | 39 |
|
24 | | - for (let i = 0; i < 6; i++) { |
25 | | - const currentTrafficLightState = getCurrentTrafficLightState(trafficLight); |
26 | | - console.log("The traffic light is on", currentTrafficLightState); |
| 40 | + for (let cycle = 0; cycle < 6; cycle++) { |
| 41 | + const currentState = getCurrentState(trafficLight); |
| 42 | + console.log(cycle, 'The traffic light is now', currentState); |
27 | 43 |
|
28 | | - trafficLight.currentStateIndex = getNextStateIndex(trafficLight); |
| 44 | + waitSync(1); // Wait a second before going to the next state |
| 45 | + trafficLight.stateIndex = getNextStateIndex(trafficLight); |
29 | 46 | } |
30 | 47 | } |
31 | 48 |
|
32 | 49 | main(); |
33 | 50 | /** |
34 | 51 | * The output should be: |
35 | 52 |
|
36 | | -The traffic light is on green |
37 | | -The traffic light is on orange |
38 | | -The traffic light is on red |
39 | | -The traffic light is on green |
40 | | -The traffic light is on orange |
41 | | -The traffic light is on red |
| 53 | +0 The traffic light is now green |
| 54 | +1 The traffic light is now orange |
| 55 | +2 The traffic light is now red |
| 56 | +3 The traffic light is now green |
| 57 | +4 The traffic light is now orange |
| 58 | +5 The traffic light is now red |
42 | 59 |
|
43 | 60 | */ |
0 commit comments