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

Skip to content

Commit c594a87

Browse files
committed
Solved the exercise
1 parent 400c49d commit c594a87

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66
*/
77

88
function getCurrentState(trafficLight) {
9+
return trafficLight.possibleStates[trafficLight.stateIndex]; // return the current state
910
// TODO
1011
// Should return the current state (i.e. colour) of the `trafficLight`
1112
// object passed as a parameter.
1213
}
1314

1415
function getNextStateIndex(trafficLight) {
16+
if (trafficLight.stateIndex === trafficLight.possibleStates.length - 1) {
17+
return 0; // If the current state is red(second index) return to the beginning (green)
18+
} else {
19+
return trafficLight.stateIndex + 1; // Otherwise increment the index to switch to next colour
20+
}
1521
// TODO
1622
// Return the index of the next state of the `trafficLight` such that:
1723
// - if the color is green, it will turn to orange

0 commit comments

Comments
 (0)