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