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

Skip to content

Commit eeb5127

Browse files
committed
Prep exersises from 2d week of JS
1 parent 92fb94c commit eeb5127

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function getCurrentState(trafficLight) {
99
// TODO
1010
// Should return the current state (i.e. colour) of the `trafficLight`
1111
// object passed as a parameter.
12+
return trafficLight.possibleStates[trafficLight.stateIndex];
1213
}
1314

1415
function getNextStateIndex(trafficLight) {
@@ -17,8 +18,14 @@ function getNextStateIndex(trafficLight) {
1718
// - if the color is green, it will turn to orange
1819
// - if the color is orange, it will turn to red
1920
// - if the color is red, it will turn to green
21+
if (getCurrentState(trafficLight) === "green") {
22+
return 1;
23+
} else if (getCurrentState(trafficLight) === "orange") {
24+
return 2;
25+
} else if (getCurrentState(trafficLight) === "red") {
26+
return 0;
27+
}
2028
}
21-
2229
// This function loops for the number of seconds specified by the `secs`
2330
// parameter and then returns.
2431
// IMPORTANT: This is not the recommended way to implement 'waiting' in

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

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,39 @@
22

33
function runExperiment(sampleSize) {
44
const valueCounts = [0, 0, 0, 0, 0, 0];
5-
6-
// TODO
7-
// Write a for loop that iterates `sampleSize` times (sampleSize is a number).
8-
// In each loop iteration:
9-
//
10-
// 1. Generate a random integer between 1 and 6 (as if throwing a six-sided die).
11-
// 2. Add `1` to the element of the `valueCount` that corresponds to the random
12-
// value from the previous step. Use the first element of `valueCounts`
13-
// for keeping a count how many times the value 1 is thrown, the second
14-
// element for value 2, etc.
5+
for (let i = 0; i < sampleSize; i++) {
6+
const x = Math.floor(Math.random() * 6);
7+
valueCounts[x]++;
8+
}
159

1610
const results = [];
1711

18-
// TODO
19-
// Write a for..of loop for the `valueCounts` array created in the previous
20-
// loop. In each loop iteration:
21-
// 1. For each possible value of the die (1-6), compute the percentage of how
22-
// many times that value was thrown. Remember that the first value of
23-
// `valueCounts` represent the die value of 1, etc.
24-
// 2. Convert the computed percentage to a number string with a precision of
25-
// two decimals, e.g. '14.60'.
26-
// 3. Then push that string onto the `results` array.
12+
for (const count of valueCounts) {
13+
const percent = (count / sampleSize) * 100;
14+
results.push(percent.toFixed(2));
15+
}
2716

2817
return results;
2918
}
3019

3120
function main() {
3221
const sampleSizes = [100, 1000, 1000000];
3322

34-
// TODO
35-
// Write a for..of loop that calls the `runExperiment()` function for each
36-
// value of the `sampleSizes` array.
37-
// Log the results of each experiment as well as the experiment size to the
38-
// console.
39-
// The expected output could look like this:
40-
//
41-
// [ '26.00', '17.00', '10.00', '19.00', '16.00', '12.00' ] 100
42-
// [ '14.60', '17.10', '19.30', '15.50', '16.70', '16.80' ] 1000
43-
// [ '16.71', '16.68', '16.69', '16.66', '16.67', '16.59' ] 1000000
23+
for (const size of sampleSizes) {
24+
const experimentResults = runExperiment(size);
25+
console.log(experimentResults, size);
26+
}
4427
}
4528

29+
// TODO
30+
// Write a for..of loop that calls the `runExperiment()` function for each
31+
// value of the `sampleSizes` array.
32+
// Log the results of each experiment as well as the experiment size to the
33+
// console.
34+
// The expected output could look like this:
35+
//
36+
// [ '26.00', '17.00', '10.00', '19.00', '16.00', '12.00' ] 100
37+
// [ '14.60', '17.10', '19.30', '15.50', '16.70', '16.80' ] 1000
38+
// [ '16.71', '16.68', '16.69', '16.66', '16.67', '16.59' ] 1000000
39+
4640
main();

0 commit comments

Comments
 (0)