diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light-2.js b/Week2/prep-exercises/1-traffic-light/traffic-light-2.js index 8c6ba95..5d9da93 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light-2.js @@ -5,19 +5,26 @@ * 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); + 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 (currentState === "green") trafficLight.stateIndex = 1; + if (currentState === "orange") trafficLight.stateIndex = 2; + if (currentState === "red") { + trafficLight.stateIndex = 0; + cycle++; + } + + // 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 } /** diff --git a/Week3/prep-exercises/1-traffic-light/traffic-light.js b/Week3/prep-exercises/1-traffic-light/traffic-light.js index f4a5c1a..d8f6df3 100644 --- a/Week3/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week3/prep-exercises/1-traffic-light/traffic-light.js @@ -6,17 +6,28 @@ */ 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]; + // TODO + // Should return the current state (i.e. colour) of the `trafficLight` + // object passed as a parameter. } 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 + let current = getCurrentState(trafficLight); + + switch (current) { + case "green": + return 1; + case "orange": + return 2; + case "red": + return 0; + } + // 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` @@ -25,25 +36,26 @@ function getNextStateIndex(trafficLight) { // JavaScript. You will learn better ways of doing this when you learn about // asynchronous code. function waitSync(secs) { - const start = Date.now(); - while (Date.now() - start < secs * 1000) { - // nothing do to here - } + const start = Date.now(); + + while (Date.now() - start < secs * 1000) { + // nothing do to here + } } 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); // Wait a second before going to the next state - trafficLight.stateIndex = getNextStateIndex(trafficLight); - } + 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); // Wait a second before going to the next state + trafficLight.stateIndex = getNextStateIndex(trafficLight); + } } main(); diff --git a/Week3/prep-exercises/2-experiments/index.js b/Week3/prep-exercises/2-experiments/index.js index 7e5aa92..c84aa2e 100644 --- a/Week3/prep-exercises/2-experiments/index.js +++ b/Week3/prep-exercises/2-experiments/index.js @@ -1,46 +1,59 @@ "use strict"; 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. - - 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. - - return results; + 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) { + let randIdx = Math.floor(Math.random() * valueCounts.length); + valueCounts[randIdx] += 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. + let sum = valueCounts.reduce((a, b) => a + b); + + for (let i = 0; i < valueCounts.length; ++i) { + results.push(((valueCounts[i] * 100) / sum).toFixed(2).toString()); + } + + 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 + const sampleSizes = [100, 1000, 1000000]; + + runExperiment(100); + // 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) { + console.log(runExperiment(sampleSize), sampleSize); + } } main();