Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eef8742

Browse files
authored
Solved traffic-light-2.js
1 parent 6144e20 commit eef8742

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

Week1/prep-exercises/1-traffic-light/traffic-light-2.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
"use strict";
2+
/**
3+
* The `possibleStates` property define the states (in this case: colours)
4+
* in which the traffic light can be.
5+
* The `stateIndex` property indicates which of the possible states is current.
6+
*/
7+
"use strict";
28
/**
39
* The `possibleStates` property define the states (in this case: colours)
410
* in which the traffic light can be.
@@ -16,9 +22,30 @@ while (cycle < 2) {
1622

1723
// TODO
1824
// if the color is green, turn it orange
25+
if (currentState === "green") {
26+
trafficLight.stateIndex = 1;
27+
}
1928
// if the color is orange, turn it red
20-
// if the color is red, add 1 to cycles and turn it green
29+
else if (currentState === "orange") {
30+
trafficLight.stateIndex = 2;
31+
}
32+
// if the color is red, add 1 to rotations and turn it green
33+
else {
34+
trafficLight.stateIndex = 0;
35+
cycle++;
36+
}
2137
}
38+
/**
39+
* The output should be:
40+
41+
The traffic light is on green
42+
The traffic light is on orange
43+
The traffic light is on red
44+
The traffic light is on green
45+
The traffic light is on orange
46+
The traffic light is on red
47+
48+
*/
2249

2350
/**
2451
* The output should be:

0 commit comments

Comments
 (0)