diff --git a/.DS_Store b/.DS_Store index 5008ddf..62ff0fe 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/Week1/.DS_Store b/Week1/.DS_Store new file mode 100644 index 0000000..b3f8745 Binary files /dev/null and b/Week1/.DS_Store differ diff --git a/Week1/prep-exercises/.DS_Store b/Week1/prep-exercises/.DS_Store new file mode 100644 index 0000000..550fbe4 Binary files /dev/null and b/Week1/prep-exercises/.DS_Store differ 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..afb1ad2 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -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" ; + } } /** 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..115e45e 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -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 + */ diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light.js b/Week2/prep-exercises/1-traffic-light/traffic-light.js index f4a5c1a..04b6fc3 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light.js @@ -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 diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index 7e5aa92..8d5fd7c 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -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; }