-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtraffic-light-1.js
More file actions
41 lines (34 loc) · 890 Bytes
/
traffic-light-1.js
File metadata and controls
41 lines (34 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"use strict";
/**
* The `state` property says what the traffic light's state (i.e. colour) is at
* that moment.
*/
const trafficLight = {
state: "green",
};
let rotations = 0;
while (rotations < 2) {
const currentState = trafficLight.state;
console.log("The traffic light is on", currentState);
if (currentState === 'green') {
trafficLight.state = 'orange';
} else if (currentState === 'orange') {
trafficLight.state = 'red';
} else {
rotations++
trafficLight.state = 'green';
}
}
// TODO
// if the color is green, turn it orange
// if the color is orange, turn it red
// if the color is red, add 1 to rotations and turn it green
/**
* The output should be:
The traffic light is on green
The traffic light is on orange
The traffic light is on red
The traffic light is on green
The traffic light is on orange
The traffic light is on red
*/