Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2b96db1

Browse files
committed
week2 prep
1 parent bad0fc1 commit 2b96db1

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

Week2/prep-exercises/1-traffic-light/traffic-light.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@
77

88
function getCurrentState(trafficLight) {
99
// TODO
10-
// Should return the current state (i.e. colour) of the `trafficLight`
10+
return(trafficLight.possibleStates[trafficLight.stateIndex]);
1111
// object passed as a parameter.
1212
}
1313

1414
function getNextStateIndex(trafficLight) {
1515
// TODO
16-
// Return the index of the next state of the `trafficLight` such that:
17-
// - if the color is green, it will turn to orange
18-
// - if the color is orange, it will turn to red
19-
// - if the color is red, it will turn to green
16+
17+
let nextIndex = 0;
18+
const currentState = getCurrentState(trafficLight);
19+
if(currentState === 'green'){
20+
return(nextIndex += 1);
21+
}else if (currentState === 'orange'){
22+
return(nextIndex += 2);
23+
}else return (nextIndex)
2024
}
2125

2226
// This function loops for the number of seconds specified by the `secs`

Week2/prep-exercises/2-experiments/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ function runExperiment(sampleSize) {
66
// TODO
77
// Write a for loop that iterates `sampleSize` times (sampleSize is a number).
88
// In each loop iteration:
9-
//
9+
for(let i = 0; i < sampleSize; i++){
10+
const randomNum = Math.floor((Math.random()*6) + 1);
11+
valueCounts[randomNum-1] += 1;
12+
}
1013
// 1. Generate a random integer between 1 and 6 (as if throwing a six-sided die).
1114
// 2. Add `1` to the element of the `valueCount` that corresponds to the random
1215
// value from the previous step. Use the first element of `valueCounts`
@@ -16,6 +19,11 @@ function runExperiment(sampleSize) {
1619
const results = [];
1720

1821
// TODO
22+
for(let count of valueCounts){
23+
let countPer = ((count / sampleSize)*100).toFixed(2)
24+
results.push(countPer);
25+
}
26+
1927
// Write a for..of loop for the `valueCounts` array created in the previous
2028
// loop. In each loop iteration:
2129
// 1. For each possible value of the die (1-6), compute the percentage of how
@@ -25,13 +33,17 @@ function runExperiment(sampleSize) {
2533
// two decimals, e.g. '14.60'.
2634
// 3. Then push that string onto the `results` array.
2735

28-
return results;
36+
return (results);
2937
}
3038

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

3442
// TODO
43+
for(let sample of sampleSizes){
44+
console.log(runExperiment(sample),sample);
45+
}
46+
3547
// Write a for..of loop that calls the `runExperiment()` function for each
3648
// value of the `sampleSizes` array.
3749
// Log the results of each experiment as well as the experiment size to the

0 commit comments

Comments
 (0)