33function runExperiment ( sampleSize ) {
44 const valueCounts = [ 0 , 0 , 0 , 0 , 0 , 0 ] ;
55
6- // TODO
6+ for ( let i = 0 ; i < sampleSize ; i ++ ) {
7+ const random = Math . floor ( ( Math . random ( ) * 6 ) + 1 ) ;
8+ valueCounts [ random - 1 ] += 1 ; //valueCount[random-1]+=1;
9+ }
710 // Write a for loop that iterates `sampleSize` times (sampleSize is a number).
811 // In each loop iteration:
912 //
@@ -12,10 +15,12 @@ function runExperiment(sampleSize) {
1215 // value from the previous step. Use the first element of `valueCounts`
1316 // for keeping a count how many times the value 1 is thrown, the second
1417 // element for value 2, etc.
15-
18+
19+
1620 const results = [ ] ;
17-
18- // TODO
21+ for ( let i of valueCounts ) {
22+ results . push ( ( i / sampleSize * 100 ) . toFixed ( 2 ) ) ; // percentage= (i/sampleSize*100).toFixed(2)
23+ }
1924 // Write a for..of loop for the `valueCounts` array created in the previous
2025 // loop. In each loop iteration:
2126 // 1. For each possible value of the die (1-6), compute the percentage of how
@@ -30,12 +35,16 @@ function runExperiment(sampleSize) {
3035
3136function main ( ) {
3237 const sampleSizes = [ 100 , 1000 , 1000000 ] ;
33-
34- // TODO
38+
39+ for ( let i of sampleSizes ) {
40+ console . log ( runExperiment ( i ) ) ;
41+ }
3542 // Write a for..of loop that calls the `runExperiment()` function for each
3643 // value of the `sampleSizes` array.
3744 // Log the results of each experiment as well as the experiment size to the
3845 // console.
46+
47+
3948 // The expected output could look like this:
4049 //
4150 // [ '26.00', '17.00', '10.00', '19.00', '16.00', '12.00' ] 100
0 commit comments