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

Skip to content

Commit 6bc8ee9

Browse files
committed
challenge solution
1 parent 61e010f commit 6bc8ee9

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,38 @@
44
* that moment.
55
*/
66
const trafficLight = {
7-
state: "green",
8-
};
7+
state: "green"};
98

109
let rotations = 0;
11-
while (rotations < 2) {
12-
const currentState = trafficLight.state;
10+
while (rotations <2) {
11+
let currentState = trafficLight.state;
1312
console.log("The traffic light is on", currentState);
1413

14+
15+
1516
// TODO
1617
// if the color is green, turn it orange
1718
// if the color is orange, turn it red
1819
// if the color is red, add 1 to rotations and turn it green
19-
}
20+
21+
switch (currentState) {
22+
23+
case "green":
24+
trafficLight.state = "orange";
25+
break;
26+
27+
case "orange":
28+
trafficLight.state = "red";
29+
break;
30+
31+
case "red":
32+
trafficLight.state = "green";
33+
rotations ++;
34+
35+
36+
}}
37+
38+
2039

2140
/**
2241
* The output should be:
@@ -28,4 +47,4 @@ The traffic light is on green
2847
The traffic light is on orange
2948
The traffic light is on red
3049
31-
*/
50+
*/

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,32 @@ while (cycle < 2) {
1414
const currentState = trafficLight.possibleStates[trafficLight.stateIndex];
1515
console.log("The traffic light is on", currentState);
1616

17+
switch(currentState) {
18+
19+
case "green":
20+
trafficLight.stateIndex++;
21+
break;
22+
23+
case "orange":
24+
trafficLight.stateIndex++;
25+
break;
26+
27+
case "red":
28+
trafficLight.stateIndex=0;
29+
cycle++;
30+
31+
}
32+
33+
34+
}
35+
36+
37+
1738
// TODO
1839
// if the color is green, turn it orange
1940
// if the color is orange, turn it red
2041
// if the color is red, add 1 to cycles and turn it green
21-
}
42+
2243

2344
/**
2445
* The output should be:

0 commit comments

Comments
 (0)