|
| 1 | +// "use strict"; |
| 2 | + |
| 3 | +// 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; |
| 29 | +// } |
| 30 | + |
| 31 | +// 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 |
| 44 | +// } |
| 45 | + |
| 46 | +// main(); |
| 47 | + |
1 | 48 | "use strict";
|
2 | 49 |
|
3 | 50 | function runExperiment(sampleSize) {
|
4 | 51 | const valueCounts = [0, 0, 0, 0, 0, 0];
|
5 | 52 |
|
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. |
| 53 | + // Step 1: Generate random dice rolls and count occurrences |
| 54 | + for (let i = 0; i < sampleSize; i++) { |
| 55 | + const roll = Math.floor(Math.random() * 6) + 1; // Generate a random number between 1 and 6 |
| 56 | + valueCounts[roll - 1]++; // Increment the corresponding count in valueCounts |
| 57 | + } |
15 | 58 |
|
16 | 59 | const results = [];
|
17 | 60 |
|
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. |
| 61 | + // Step 2: Calculate the percentage of each value and format the results |
| 62 | + for (let count of valueCounts) { |
| 63 | + const percentage = ((count / sampleSize) * 100).toFixed(2); // Calculate the percentage and fix to 2 decimal places |
| 64 | + results.push(percentage); // Push the formatted percentage to the results array |
| 65 | + } |
27 | 66 |
|
28 | 67 | return results;
|
29 | 68 | }
|
30 | 69 |
|
31 | 70 | function main() {
|
32 | 71 | const sampleSizes = [100, 1000, 1000000];
|
33 | 72 |
|
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 |
| 73 | + // Run experiments for different sample sizes and log the results |
| 74 | + for (let sampleSize of sampleSizes) { |
| 75 | + const percentages = runExperiment(sampleSize); |
| 76 | + console.log(percentages, sampleSize); |
| 77 | + } |
44 | 78 | }
|
45 | 79 |
|
46 | 80 | main();
|
0 commit comments