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

Skip to content

Commit ecec8ef

Browse files
committed
solution for traffic-light task week 2
minor corrections for traffic-light task week 1
1 parent 6abbb6c commit ecec8ef

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

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

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@
44
* in which the traffic light can be.
55
* The `stateIndex` property indicates which of the possible states is current.
66
*/
7-
"use strict";
8-
/**
9-
* The `possibleStates` property define the states (in this case: colours)
10-
* in which the traffic light can be.
11-
* The `stateIndex` property indicates which of the possible states is current.
12-
*/
7+
138
const trafficLight = {
149
possibleStates: ["green", "orange", "red"],
1510
stateIndex: 0,
@@ -46,15 +41,3 @@ The traffic light is on orange
4641
The traffic light is on red
4742
4843
*/
49-
50-
/**
51-
* The output should be:
52-
53-
The traffic light is on green
54-
The traffic light is on orange
55-
The traffic light is on red
56-
The traffic light is on green
57-
The traffic light is on orange
58-
The traffic light is on red
59-
60-
*/

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,29 @@
77

88
function getCurrentState(trafficLight) {
99
// TODO
10-
// Should return the current state (i.e. colour) of the `trafficLight`
10+
// Should return the current state (i.e. color) of the `trafficLight`
1111
// object passed as a parameter.
12+
return trafficLight.possibleStates[trafficLight.stateIndex];
1213
}
1314

1415
function getNextStateIndex(trafficLight) {
1516
// TODO
1617
// 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-
}
18+
19+
// if the color is green, turn it orange
20+
if (trafficLight.stateIndex === 0) {
21+
return 1;
22+
}
23+
// if the color is orange, turn it red
24+
else if (trafficLight.stateIndex === 1) {
25+
return 2;
26+
}
27+
// if the color is red, add 1 to rotations and turn it green
28+
else {
29+
return 0;
30+
}
31+
}
32+
2133

2234
// This function loops for the number of seconds specified by the `secs`
2335
// parameter and then returns.
@@ -38,7 +50,7 @@ function main() {
3850
};
3951

4052
for (let cycle = 0; cycle < 6; cycle++) {
41-
const currentState = getCurrentState(trafficLight);
53+
let currentState = getCurrentState(trafficLight);
4254
console.log(cycle, "The traffic light is now", currentState);
4355

4456
waitSync(1); // Wait a second before going to the next state

0 commit comments

Comments
 (0)