Skip to content

Commit 4209363

Browse files
committed
Finished experiments exercise
1 parent 63fd252 commit 4209363

File tree

1 file changed

+50
-37
lines changed
  • Week3/prep-exercises/2-experiments

1 file changed

+50
-37
lines changed
Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,59 @@
11
"use strict";
22

33
function runExperiment(sampleSize) {
4-
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.
15-
16-
const results = [];
17-
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.
27-
28-
return results;
4+
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.
15+
for (let i = 0; i < sampleSize; ++i) {
16+
let randIdx = Math.floor(Math.random() * valueCounts.length);
17+
valueCounts[randIdx] += 1;
18+
}
19+
20+
const results = [];
21+
22+
// TODO
23+
// Write a for..of loop for the `valueCounts` array created in the previous
24+
// loop. In each loop iteration:
25+
// 1. For each possible value of the die (1-6), compute the percentage of how
26+
// many times that value was thrown. Remember that the first value of
27+
// `valueCounts` represent the die value of 1, etc.
28+
// 2. Convert the computed percentage to a number string with a precision of
29+
// two decimals, e.g. '14.60'.
30+
// 3. Then push that string onto the `results` array.
31+
let sum = valueCounts.reduce((a, b) => a + b);
32+
33+
for (let i = 0; i < valueCounts.length; ++i) {
34+
results.push(((valueCounts[i] * 100) / sum).toFixed(2).toString());
35+
}
36+
37+
return results;
2938
}
3039

3140
function main() {
32-
const sampleSizes = [100, 1000, 1000000];
33-
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
41+
const sampleSizes = [100, 1000, 1000000];
42+
43+
runExperiment(100);
44+
// TODO
45+
// Write a for..of loop that calls the `runExperiment()` function for each
46+
// value of the `sampleSizes` array.
47+
// Log the results of each experiment as well as the experiment size to the
48+
// console.
49+
// The expected output could look like this:
50+
//
51+
// [ '26.00', '17.00', '10.00', '19.00', '16.00', '12.00' ] 100
52+
// [ '14.60', '17.10', '19.30', '15.50', '16.70', '16.80' ] 1000
53+
// [ '16.71', '16.68', '16.69', '16.66', '16.67', '16.59' ] 1000000
54+
for (const sampleSize of sampleSizes) {
55+
console.log(runExperiment(sampleSize), sampleSize);
56+
}
4457
}
4558

4659
main();

0 commit comments

Comments
 (0)