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

Skip to content

Commit ca25fce

Browse files
committed
added changes to project
1 parent c064546 commit ca25fce

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,36 @@ function getCurrentState(trafficLight) {
99
// TODO
1010
// Should return the current state (i.e. colour) of the `trafficLight`
1111
// object passed as a parameter.
12+
return trafficLight.possibleStates[trafficLight.stateIndex];
13+
1214
}
13-
1415
function getNextStateIndex(trafficLight) {
1516
// TODO
1617
// Return the index of the next state of the `trafficLight` such that:
1718
// - if the color is green, it will turn to orange
1819
// - if the color is orange, it will turn to red
1920
// - if the color is red, it will turn to green
20-
}
21+
2122

2223
// This function loops for the number of seconds specified by the `secs`
2324
// parameter and then returns.
2425
// IMPORTANT: This is not the recommended way to implement 'waiting' in
2526
// JavaScript. You will learn better ways of doing this when you learn about
2627
// asynchronous code.
28+
const currentState = trafficLight.stateIndex;
29+
30+
if ( currentState === 0 ){
31+
return 1;
32+
}
33+
else if (currentState === 1 ){
34+
return 2;
35+
}
36+
else if (currentState === 2 ){
37+
return 0;
38+
}}
39+
40+
41+
2742
function waitSync(secs) {
2843
const start = Date.now();
2944
while (Date.now() - start < secs * 1000) {

0 commit comments

Comments
 (0)