@@ -8,8 +8,7 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
88// It will help to think about the order in which expressions are evaluated
99// Try logging the value of num and running the program several times to build an idea of what the program is doing
1010
11-
12- console . log ( num )
11+ console . log ( num ) ;
1312
1413// num returns a value of 60, running it again returns a value of 15, looking at the code I see a .random() function
1514// I can make a hypothesis that this returns a random number of some kind, hence the inconsistent result
@@ -18,25 +17,25 @@ console.log(num)
1817// Code in inner-most brackets is evaluated first
1918// I would expect (maximum - minimum + 1) to be evaluated first (100 - 1 + 1) would equal 100
2019
21- console . log ( maximum - minimum + 1 )
20+ console . log ( maximum - minimum + 1 ) ;
2221
2322// the result was 100
2423// this part of the code is then multiplied by Math.random()
2524
26- console . log ( Math . random ( ) )
25+ console . log ( Math . random ( ) ) ;
2726
2827// running this code many times, it appears Math.random() returns a random number between 0 and 1
2928// checking documentation online, this is correct (actually between 0 and 0.99999999999)
3029// therefore Math.random() * (maximum - minimum + 1) should return a random number between 1 and 100
3130
32- console . log ( Math . random ( ) * ( maximum - minimum + 1 ) )
31+ console . log ( Math . random ( ) * ( maximum - minimum + 1 ) ) ;
3332
3433// It does, it returns it with many decimal places, the result of num didn't have any decimal places
3534// The next part of the code that is run is Math.floor, let's see what this does
3635
37- const number = Math . random ( ) * ( maximum - minimum + 1 )
38- console . log ( number )
39- console . log ( Math . floor ( number ) )
36+ const number = Math . random ( ) * ( maximum - minimum + 1 ) ;
37+ console . log ( number ) ;
38+ console . log ( Math . floor ( number ) ) ;
4039
4140// because Math.random() creates a new random number every time it is run, I had to set the
4241// result to a variable so I can use the same number in multiple functions
@@ -48,4 +47,4 @@ console.log(Math.floor(number))
4847// The final part is the addition of minimum (+1)
4948
5049// Therefore I can see this code produces a random whole integer between minimum and maximum
51- // Which in this case is an integer between 1 and 100
50+ // Which in this case is an integer between 1 and 100
0 commit comments