|
1 |
| -"use strict"; |
| 1 | +'use strict'; |
2 | 2 | /**
|
3 | 3 | * The `possibleStates` property define the states (in this case: colours)
|
4 | 4 | * in which the traffic light can be.
|
5 | 5 | * The `stateIndex` property indicates which of the possible states is current.
|
6 | 6 | */
|
7 | 7 | const trafficLight = {
|
8 |
| - possibleStates: ["green", "orange", "red"], |
| 8 | + possibleStates: ['green', 'orange', 'red'], |
9 | 9 | stateIndex: 0,
|
10 | 10 | };
|
11 | 11 |
|
12 | 12 | let cycle = 0;
|
| 13 | + |
13 | 14 | while (cycle < 2) {
|
14 | 15 | const currentState = trafficLight.possibleStates[trafficLight.stateIndex];
|
15 |
| - console.log("The traffic light is on", currentState); |
| 16 | + |
| 17 | + if (currentState == 'green') { |
| 18 | + trafficLight.stateIndex = 1; |
| 19 | + } else if (currentState == 'orange') { |
| 20 | + trafficLight.stateIndex = 2; |
| 21 | + } else if (currentState == 'red') { |
| 22 | + trafficLight.stateIndex = 0; |
| 23 | + cycle++; |
| 24 | + } |
| 25 | + console.log('The traffic light is on', currentState); |
16 | 26 |
|
17 | 27 | // TODO
|
18 | 28 | // if the color is green, turn it orange
|
19 | 29 | // if the color is orange, turn it red
|
20 | 30 | // if the color is red, add 1 to cycles and turn it green
|
21 | 31 | }
|
22 | 32 |
|
| 33 | +// do while loop |
| 34 | + |
| 35 | +// do { |
| 36 | +// const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; |
| 37 | +// if (currentState == 'green') { |
| 38 | +// trafficLight.stateIndex = 1 |
| 39 | +// } else if (currentState == "orange") { |
| 40 | +// trafficLight.stateIndex = 2 |
| 41 | +// } else if (currentState == 'red') { |
| 42 | +// trafficLight.stateIndex = 0 |
| 43 | +// cycle++ |
| 44 | +// } |
| 45 | +// console.log("The traffic light is on", currentState); |
| 46 | + |
| 47 | +// } while (cycle < 2) |
| 48 | + |
| 49 | +//for loop |
| 50 | + |
| 51 | +// for (let i = 0; i < 2;) { |
| 52 | +// const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; |
| 53 | +// if (currentState == 'green') { |
| 54 | +// trafficLight.stateIndex = 1 |
| 55 | +// } else if (currentState == "orange") { |
| 56 | +// trafficLight.stateIndex = 2 |
| 57 | +// } else if (currentState == 'red') { |
| 58 | +// trafficLight.stateIndex = 0 |
| 59 | +// i++ |
| 60 | +// } |
| 61 | +// console.log("The traffic light is on", currentState); |
| 62 | + |
| 63 | +// } |
| 64 | + |
| 65 | +// In a for loop, I use i instead of cycle, so I treat it like a cycle. I want to increase it when the color is red. |
| 66 | + |
23 | 67 | /**
|
24 | 68 | * The output should be:
|
25 | 69 |
|
|
0 commit comments