From 46b1b28145ab87660a03c829a445625dfd0826c3 Mon Sep 17 00:00:00 2001 From: Etem Date: Sat, 15 Feb 2025 14:38:49 +0100 Subject: [PATCH 1/5] Completed traffic light exercise --- .../1-traffic-light/traffic-light-1.js | 33 +++++++------------ 1 file changed, 11 insertions(+), 22 deletions(-) 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 - -*/ From e39ae3cc196adca03957dcd73cf71af0ecde2bc6 Mon Sep 17 00:00:00 2001 From: Etem Date: Sat, 15 Feb 2025 14:55:40 +0100 Subject: [PATCH 2/5] Completed traffic light exercise --- .../1-traffic-light/traffic-light-2.js | 44 ++++++------------- 1 file changed, 14 insertions(+), 30 deletions(-) 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 From 5d769cfe29ea82828d5aeac942f9fba360d6de5c Mon Sep 17 00:00:00 2001 From: Etem Date: Sat, 22 Feb 2025 12:55:06 +0100 Subject: [PATCH 3/5] Add traffic light simulation --- README.md | 9 +++++++++ traffic-light.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 README.md create mode 100644 traffic-light.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..b3c3748 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# 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? 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(); From 2bcb1ad9127f78b8f2dfba3d2472f5c743c2936d Mon Sep 17 00:00:00 2001 From: Etem Date: Sat, 22 Feb 2025 23:49:40 +0100 Subject: [PATCH 4/5] add experiment exercises --- Week2/prep-exercises/2-experiments/index.js | 54 +++++++++------------ 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index 7e5aa92..e7891e9 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -3,44 +3,38 @@ 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(); From 03fbd63f6887239b9709a2efc4546a3b102989b5 Mon Sep 17 00:00:00 2001 From: Etem Date: Sat, 22 Feb 2025 23:58:38 +0100 Subject: [PATCH 5/5] added info at the top of the code page --- Week2/prep-exercises/2-experiments/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index e7891e9..eacc133 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -1,5 +1,7 @@ "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];