Skip to content

Amani Prep_ex-week2 #69

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 6 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
Binary file modified .DS_Store
Binary file not shown.
Binary file added Week1/.DS_Store
Binary file not shown.
Binary file added Week1/prep-exercises/.DS_Store
Binary file not shown.
13 changes: 9 additions & 4 deletions Week1/prep-exercises/1-traffic-light/traffic-light-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ 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") {
rotations++;
trafficLight.state= "green" ;
}
}

/**
Expand Down
38 changes: 21 additions & 17 deletions Week1/prep-exercises/1-traffic-light/traffic-light-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,32 @@
*/
const trafficLight = {
possibleStates: ["green", "orange", "red"],
stateIndex: 0,
stateIndex: 0,
};

localStorage
let cycle = 0;
while (cycle < 2) {

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

if (trafficLight.stateIndex === 0) {
trafficLight.stateIndex = 1;
} else if (trafficLight.stateIndex === 1) {
trafficLight.stateIndex = 2;
} else if (trafficLight.stateIndex === 2) {
cycle++;
trafficLight.stateIndex = 0;
}
}

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

*/
* The expected output:
* 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
*/
12 changes: 4 additions & 8 deletions Week2/prep-exercises/1-traffic-light/traffic-light.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@
*/

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

return (trafficLight.stateIndex + 1) % trafficLight.possibleStates.length;
}


// 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
Expand Down
29 changes: 10 additions & 19 deletions Week2/prep-exercises/2-experiments/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,18 @@
function runExperiment(sampleSize) {
const valueCounts = [0, 0, 0, 0, 0, 0];

// TODO
// Write a for loop that iterates `sampleSize` times (sampleSize is a number).
// In each loop iteration:
//
// 1. Generate a random integer between 1 and 6 (as if throwing a six-sided die).
// 2. Add `1` to the element of the `valueCount` that corresponds to the random
// value from the previous step. Use the first element of `valueCounts`
// for keeping a count how many times the value 1 is thrown, the second
// element for value 2, etc.

for (let i = 0; i < sampleSize; i++) {
const roll = Math.floor(Math.random() * 6);

valueCounts[roll]++;
}
const results = [];

// TODO
// Write a for..of loop for the `valueCounts` array created in the previous
// loop. In each loop iteration:
// 1. For each possible value of the die (1-6), compute the percentage of how
// many times that value was thrown. Remember that the first value of
// `valueCounts` represent the die value of 1, etc.
// 2. Convert the computed percentage to a number string with a precision of
// two decimals, e.g. '14.60'.
// 3. Then push that string onto the `results` array.
for (const count of valueCounts) {
const percentage = ((count / sampleSize) * 100).toFixed(2);
results.push(percentage);
}


return results;
}
Expand Down