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

Skip to content

Commit 4ed4f8c

Browse files
committed
prep exercises from week2 is completed
1 parent 30024ba commit 4ed4f8c

File tree

1 file changed

+16
-0
lines changed
  • Week2/prep-exercises/2-experiments

1 file changed

+16
-0
lines changed

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

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

1822
// TODO
23+
for (const count of valueCounts) {
24+
const percentage = (count / sampleSize) * 100;
25+
const formattedPercentage = percentage.toFixed(2);
26+
results.push(formattedPercentage);
27+
}
1928
// Write a for..of loop for the `valueCounts` array created in the previous
2029
// loop. In each loop iteration:
2130
// 1. For each possible value of the die (1-6), compute the percentage of how
@@ -28,9 +37,16 @@ function runExperiment(sampleSize) {
2837
return results;
2938
}
3039

40+
3141
function main() {
3242
const sampleSizes = [100, 1000, 1000000];
43+
// Run experiments for each sample size
44+
for (const size of sampleSizes) {
45+
const results = runExperiment(size);
3346

47+
// Log the results and experiment size to the console
48+
console.log(results, size);
49+
}
3450
// TODO
3551
// Write a for..of loop that calls the `runExperiment()` function for each
3652
// value of the `sampleSizes` array.

0 commit comments

Comments
 (0)