@@ -33,14 +33,14 @@ let count = {
3333 ' 312' : 0
3434};
3535
36- for (let i = 0 ; i < 1000000 ; i++ ) {
36+ for (let i = 0 ; i < 1000000 ; i++ ) {
3737 let array = [1 , 2 , 3 ];
3838 shuffle (array);
3939 count[array .join (' ' )]++ ;
4040}
4141
4242// show counts of all possible permutations
43- for (let key in count) {
43+ for (let key in count) {
4444 alert (` ${ key} : ${ count[key]} ` );
4545}
4646```
@@ -66,8 +66,8 @@ There are other good ways to do the task. For instance, there's a great algorith
6666
6767``` js
6868function shuffle (array ) {
69- for (let i = array .length - 1 ; i > 0 ; i-- ) {
70- let j = Math .floor (Math .random () * (i+ 1 )); // random index from 0 to i
69+ for (let i = array .length - 1 ; i > 0 ; i-- ) {
70+ let j = Math .floor (Math .random () * (i + 1 )); // random index from 0 to i
7171 [array[i], array[j]] = [array[j], array[i]]; // swap elements
7272 }
7373}
@@ -77,8 +77,8 @@ Let's test it the same way:
7777
7878``` js run
7979function shuffle (array ) {
80- for (let i = array .length - 1 ; i > 0 ; i-- ) {
81- let j = Math .floor (Math .random () * (i+ 1 ));
80+ for (let i = array .length - 1 ; i > 0 ; i-- ) {
81+ let j = Math .floor (Math .random () * (i + 1 ));
8282 [array[i], array[j]] = [array[j], array[i]];
8383 }
8484}
@@ -93,14 +93,14 @@ let count = {
9393 ' 312' : 0
9494};
9595
96- for (let i = 0 ; i < 1000000 ; i++ ) {
96+ for (let i = 0 ; i < 1000000 ; i++ ) {
9797 let array = [1 , 2 , 3 ];
9898 shuffle (array);
9999 count[array .join (' ' )]++ ;
100100}
101101
102102// show counts of all possible permutations
103- for (let key in count) {
103+ for (let key in count) {
104104 alert (` ${ key} : ${ count[key]} ` );
105105}
106106```
0 commit comments