Skip to content

Samira week1 js #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 15 additions & 20 deletions Week1/prep-exercises/1-traffic-light/traffic-light-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,24 @@
* The `state` property says what the traffic light's state (i.e. colour) is at
* that moment.
*/

const trafficLight = {
state: "green",
state : "green",
};

let rotations = 0;
while (rotations < 2) {
let rotation = 0;
while (rotation < 2) {
const currentState = trafficLight.state;
console.log("The traffic light is on", currentState);
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";

/**
* 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
}
else if (currentState === "orange") {
trafficLight.state = "red";

*/
}
else if (currentState === "red" ) {
trafficLight.state = "green";
rotation++;
}}
56 changes: 30 additions & 26 deletions Week1/prep-exercises/1-traffic-light/traffic-light-2.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
"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,
};

const trafficlight = {
possibleStates : [ "green", "orange", "red"],
stateIndex :0,
};
let cycle = 0;
while (cycle < 2) {
const currentState = trafficLight.possibleStates[trafficLight.stateIndex];
console.log("The traffic light is on", currentState);
function updateTrafficLight() {
const currentState = trafficlight.possibleStates[trafficlight.stateIndex];
const trafficLightElement = document.getElementById("traffic-light");
trafficLightElement.textContent = `The traffic light is on ${currentState}`;
trafficLightElement.className = "";
trafficLightElement.classList.add(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
}
if (currentState === "green") {
trafficlight.stateIndex = 1;

/**
* The output should be:
}
else if(currentState === "orange") {
trafficlight.stateIndex = 2;

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 === "red") {
trafficlight.stateIndex = 0;
cycle++;
}

if (cycle < 3) {
setTimeout(updateTrafficLight, 1000);

}
else {
trafficLightElement.textContent = "Traffic light simulation compeleted." ;
}
}

*/
updateTrafficLight();