Skip to content

completed prep exer #66

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 7 commits into
base: main
Choose a base branch
from
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
<<<<<<< HEAD
# Prep exercise - Traffic light

In the previous week we started with our traffic light. Now that we also know what a function is we have one last look at the workings of a traffic light in a different way. Take a look at the `traffic-light.js` file and implement the same requirements as last week again, but then with another different way of organising.

## Things to think about

- This time the loop was changed to a for loop that will run the code 6 times. Why was that needed?
- Why was the trafficLight added to the `main` function and not left at the top of the file?
- What do you think is the advantage of having the `getCurrentTrafficLightState` and `getNextStateIndex` functions?
=======
> If you are following the HackYourFuture curriculum we recommend you to start with module 1: [HTML/CSS/GIT](https://github.com/HackYourFuture/HTML-CSS). To get a complete overview of the HackYourFuture curriculum first, click [here](https://github.com/HackYourFuture/curriculum).

> Please help us improve and share your feedback! If you find better tutorials
Expand Down Expand Up @@ -91,3 +102,4 @@ If you feel ready for the next challenge, click [here](https://www.github.com/Ha
_The HackYourFuture curriculum is subject to CC BY copyright. This means you can freely use our materials, but just make sure to give us credit for it :)_

<a rel="license" href="http://creativecommons.org/licenses/by/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by/4.0/88x31.png" /></a><br />This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a>.
>>>>>>> origin/main
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

*/
44 changes: 14 additions & 30 deletions Week1/prep-exercises/1-traffic-light/traffic-light-2.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,17 @@
"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,
};

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);

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

*/
while (cycle < 2) {
const currentState = trafficLight.possibleStates[trafficLight.stateIndex];
console.log("The traffic light is on", currentState);
trafficLight.stateIndex++;
if (trafficLight.stateIndex === 3) {
trafficLight.stateIndex = 0;
cycle++;
}
}
56 changes: 26 additions & 30 deletions Week2/prep-exercises/2-experiments/index.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,42 @@
"use strict";

/* this is not my code. I found it on internet and the help og copilot and chatGPT but i didnt understand it at all. i put it here to make it obvious. */

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 dieValue = Math.floor(Math.random() * 6) + 1;
valueCounts[dieValue - 1]++;
*/
}




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 (let i = 0; i < valueCounts.length; i++) {
const percentage = (valueCounts[i] / sampleSize) * 100;
results.push(percentage.toFixed(2));
}
*/


return results;
}


function main() {
const sampleSizes = [100, 1000, 1000000];

// TODO
// Write a for..of loop that calls the `runExperiment()` function for each
// value of the `sampleSizes` array.
// Log the results of each experiment as well as the experiment size to the
// console.
// The expected output could look like this:
//
// [ '26.00', '17.00', '10.00', '19.00', '16.00', '12.00' ] 100
// [ '14.60', '17.10', '19.30', '15.50', '16.70', '16.80' ] 1000
// [ '16.71', '16.68', '16.69', '16.66', '16.67', '16.59' ] 1000000
/*
for (const sampleSize of sampleSizes) {
const results = runExperiment(sampleSize);
console.log(results, sampleSize);
}
*/

}

main();
46 changes: 46 additions & 0 deletions traffic-light.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"use strict";

function getCurrentState(trafficLight) {
return trafficLight.possibleStates[trafficLight.stateIndex];
}

function getNextStateIndex(trafficLight) {
const colorState = trafficLight.possibleStates[trafficLight.stateIndex];
if (colorState === 'green') {return 1;}
if (colorState === 'orange') {return 2;}
if (colorState === 'red') {return 0;}

/* This is also another solution I found:
if (trafficLight.stateIndex === 2) {
return 0;
}
else {
return trafficLight.stateIndex + 1;
}
*/

}


function waitSync(secs) {
const start = Date.now();
while (Date.now() - start < secs * 1000) {
}
}

function main() {
const trafficLight = {
possibleStates: ["green", "orange", "red"],
stateIndex: 0,
};

for (let cycle = 0; cycle < 6; cycle++) {
const currentState = getCurrentState(trafficLight);
console.log(cycle, "The traffic light is now", currentState);

waitSync(1);
trafficLight.stateIndex = getNextStateIndex(trafficLight);
}
}

main();