@@ -6,7 +6,10 @@ function runExperiment(sampleSize) {
66 // TODO
77 // Write a for loop that iterates `sampleSize` times (sampleSize is a number).
88 // 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+ }
1013 // 1. Generate a random integer between 1 and 6 (as if throwing a six-sided die).
1114 // 2. Add `1` to the element of the `valueCount` that corresponds to the random
1215 // value from the previous step. Use the first element of `valueCounts`
@@ -16,6 +19,11 @@ function runExperiment(sampleSize) {
1619 const results = [ ] ;
1720
1821 // TODO
22+ for ( let count of valueCounts ) {
23+ let countPer = ( ( count / sampleSize ) * 100 ) . toFixed ( 2 )
24+ results . push ( countPer ) ;
25+ }
26+
1927 // Write a for..of loop for the `valueCounts` array created in the previous
2028 // loop. In each loop iteration:
2129 // 1. For each possible value of the die (1-6), compute the percentage of how
@@ -25,13 +33,17 @@ function runExperiment(sampleSize) {
2533 // two decimals, e.g. '14.60'.
2634 // 3. Then push that string onto the `results` array.
2735
28- return results ;
36+ return ( results ) ;
2937}
3038
3139function main ( ) {
3240 const sampleSizes = [ 100 , 1000 , 1000000 ] ;
3341
3442 // TODO
43+ for ( let sample of sampleSizes ) {
44+ console . log ( runExperiment ( sample ) , sample ) ;
45+ }
46+
3547 // Write a for..of loop that calls the `runExperiment()` function for each
3648 // value of the `sampleSizes` array.
3749 // Log the results of each experiment as well as the experiment size to the
0 commit comments