diff --git a/README.md b/README.md
index a9a49e4..b709fc8 100644
--- a/README.md
+++ b/README.md
@@ -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
@@ -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 :)_

This work is licensed under a Creative Commons Attribution 4.0 International License.
+>>>>>>> origin/main
diff --git a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js
index f1d9169..ace7cd7 100644
--- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js
+++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js
@@ -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",
};
@@ -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
-
-*/
diff --git a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js
index 8c6ba95..e399257 100644
--- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js
+++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js
@@ -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++;
+ }
+}
\ No newline at end of file
diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js
index 7e5aa92..eacc133 100644
--- a/Week2/prep-exercises/2-experiments/index.js
+++ b/Week2/prep-exercises/2-experiments/index.js
@@ -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();
diff --git a/traffic-light.js b/traffic-light.js
new file mode 100644
index 0000000..8b0d473
--- /dev/null
+++ b/traffic-light.js
@@ -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();