From c06454682f6efe1c35ceacc27b9ebaadbfe49a9e Mon Sep 17 00:00:00 2001 From: Samira Date: Mon, 25 Nov 2024 10:07:03 +0100 Subject: [PATCH 1/2] update change --- .../1-traffic-light/traffic-light-1.js | 19 +++-------- .../1-traffic-light/traffic-light-2.js | 32 +++++++------------ 2 files changed, 16 insertions(+), 35 deletions(-) diff --git a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js index f1d9169..982af8a 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -11,21 +11,12 @@ let rotations = 0; while (rotations < 2) { const currentState = trafficLight.state; console.log("The traffic light is on", currentState); - - // TODO - // if the color is green, turn it orange - // if the color is orange, turn it red - // if the color is red, add 1 to rotations and turn it green } -/** - * The output should be: +if (currentState === "green") { trafficLight.state = "orange"; -The traffic light is on green -The traffic light is on orange -The traffic light is on red -The traffic light is on green -The traffic light is on orange -The traffic light is on red +} else if (currentState === "orange") { trafficLight.state = "red"; -*/ + } else if (currentState === "red") { trafficLight.state = "green"; + rotations++; + } \ No newline at end of file diff --git a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js index 8c6ba95..5429fbf 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -1,9 +1,5 @@ "use strict"; -/** - * The `possibleStates` property define the states (in this case: colours) - * in which the traffic light can be. - * The `stateIndex` property indicates which of the possible states is current. - */ + const trafficLight = { possibleStates: ["green", "orange", "red"], stateIndex: 0, @@ -14,20 +10,14 @@ while (cycle < 2) { const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; console.log("The traffic light is on", currentState); - // TODO - // if the color is green, turn it orange - // if the color is orange, turn it red - // if the color is red, add 1 to cycles and turn it green -} - -/** - * The output should be: - -The traffic light is on green -The traffic light is on orange -The traffic light is on red -The traffic light is on green -The traffic light is on orange -The traffic light is on red + if (currentState === "green") + { trafficLight.stateIndex = 1; + } else if (currentState === "orange") + { trafficLight.stateIndex = 2 ; -*/ + } else if (currentState === "red") { + trafficLight.stateIndex = 0; + + cycle++; + } + } \ No newline at end of file From ca25fcec475218a8360842ffbd7fbb6417b2434d Mon Sep 17 00:00:00 2001 From: Samira Date: Tue, 26 Nov 2024 13:25:48 +0100 Subject: [PATCH 2/2] added changes to project --- .../1-traffic-light/traffic-light.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light.js b/Week2/prep-exercises/1-traffic-light/traffic-light.js index f4a5c1a..5c29aa4 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light.js @@ -9,21 +9,36 @@ function getCurrentState(trafficLight) { // TODO // Should return the current state (i.e. colour) of the `trafficLight` // object passed as a parameter. + return trafficLight.possibleStates[trafficLight.stateIndex]; + } - function getNextStateIndex(trafficLight) { // TODO // Return the index of the next state of the `trafficLight` such that: // - if the color is green, it will turn to orange // - if the color is orange, it will turn to red // - if the color is red, it will turn to green -} + // This function loops for the number of seconds specified by the `secs` // parameter and then returns. // IMPORTANT: This is not the recommended way to implement 'waiting' in // JavaScript. You will learn better ways of doing this when you learn about // asynchronous code. +const currentState = trafficLight.stateIndex; + +if ( currentState === 0 ){ + return 1; +} +else if (currentState === 1 ){ + return 2; +} +else if (currentState === 2 ){ + return 0; +}} + + + function waitSync(secs) { const start = Date.now(); while (Date.now() - start < secs * 1000) {