Skip to content
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

Samira js 2 #58

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
19 changes: 5 additions & 14 deletions Week1/prep-exercises/1-traffic-light/traffic-light-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
32 changes: 11 additions & 21 deletions Week1/prep-exercises/1-traffic-light/traffic-light-2.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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++;
}
}
19 changes: 17 additions & 2 deletions Week2/prep-exercises/1-traffic-light/traffic-light.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down