Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Completed traffic light exercise
  • Loading branch information
benetem committed Feb 15, 2025
commit 46b1b28145ab87660a03c829a445625dfd0826c3
33 changes: 11 additions & 22 deletions Week1/prep-exercises/1-traffic-light/traffic-light-1.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
"use strict";
/**
* The `state` property says what the traffic light's state (i.e. colour) is at
* that moment.
*/
const trafficLight = {
let trafficLight = {
state: "green",
};

Expand All @@ -12,20 +7,14 @@ 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
if (currentState === "green") {
trafficLight.state = "orange";
}
else if (currentState === "orange") {
trafficLight.state = "red";
}
else if (currentState === "red") {
trafficLight.state = "green";
rotations++;
}
}

/**
* 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

*/