@@ -7,6 +7,10 @@ function runExperiment(sampleSize) {
7
7
// Write a for loop that iterates `sampleSize` times (sampleSize is a number).
8
8
// In each loop iteration:
9
9
//
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
+ }
10
14
// 1. Generate a random integer between 1 and 6 (as if throwing a six-sided die).
11
15
// 2. Add `1` to the element of the `valueCount` that corresponds to the random
12
16
// value from the previous step. Use the first element of `valueCounts`
@@ -16,6 +20,11 @@ function runExperiment(sampleSize) {
16
20
const results = [ ] ;
17
21
18
22
// TODO
23
+ for ( const count of valueCounts ) {
24
+ const percentage = ( count / sampleSize ) * 100 ;
25
+ const formattedPercentage = percentage . toFixed ( 2 ) ;
26
+ results . push ( formattedPercentage ) ;
27
+ }
19
28
// Write a for..of loop for the `valueCounts` array created in the previous
20
29
// loop. In each loop iteration:
21
30
// 1. For each possible value of the die (1-6), compute the percentage of how
@@ -28,9 +37,16 @@ function runExperiment(sampleSize) {
28
37
return results ;
29
38
}
30
39
40
+
31
41
function main ( ) {
32
42
const sampleSizes = [ 100 , 1000 , 1000000 ] ;
43
+ // Run experiments for each sample size
44
+ for ( const size of sampleSizes ) {
45
+ const results = runExperiment ( size ) ;
33
46
47
+ // Log the results and experiment size to the console
48
+ console . log ( results , size ) ;
49
+ }
34
50
// TODO
35
51
// Write a for..of loop that calls the `runExperiment()` function for each
36
52
// value of the `sampleSizes` array.
0 commit comments