22
33function runExperiment ( sampleSize ) {
44 const valueCounts = [ 0 , 0 , 0 , 0 , 0 , 0 ] ;
5+
6+ for ( let i = 0 ; i < sampleSize ; i ++ ) {
7+ let x = Math . floor ( Math . random ( ) * 6 + 1 ) ;
8+ let numberArray = [ 1 , 2 , 3 , 4 , 5 , 6 ] ;
9+ valueCounts [ numberArray . findIndex ( element => element === x ) ] ++ ;
10+
11+ }
512
613 // TODO
714 // Write a for loop that iterates `sampleSize` times (sampleSize is a number).
@@ -14,6 +21,9 @@ function runExperiment(sampleSize) {
1421 // element for value 2, etc.
1522
1623 const results = [ ] ;
24+ for ( let valueCount of valueCounts ) {
25+ results . push ( ( ( valueCount / sampleSize ) * 100 ) . toFixed ( 2 ) ) ;
26+ }
1727
1828 // TODO
1929 // Write a for..of loop for the `valueCounts` array created in the previous
@@ -24,12 +34,17 @@ function runExperiment(sampleSize) {
2434 // 2. Convert the computed percentage to a number string with a precision of
2535 // two decimals, e.g. '14.60'.
2636 // 3. Then push that string onto the `results` array.
27-
37+ console . log ( results ) ;
2838 return results ;
39+
2940}
3041
3142function main ( ) {
3243 const sampleSizes = [ 100 , 1000 , 1000000 ] ;
44+ for ( let sampleSize of sampleSizes ) {
45+ runExperiment ( sampleSize ) ;
46+
47+ }
3348
3449 // TODO
3550 // Write a for..of loop that calls the `runExperiment()` function for each
0 commit comments