@@ -3,7 +3,25 @@ const maximum = 100;
33
44const num = Math . floor ( Math . random ( ) * ( maximum - minimum + 1 ) ) + minimum ;
55
6+ console . log ( num ) ;
7+
8+
69// In this exercise, you will need to work out what num represents?
710// Try breaking down the expression and using documentation to explain what it means
811// It will help to think about the order in which expressions are evaluated
912// Try logging the value of num and running the program several times to build an idea of what the program is doing
13+
14+ Num represents a random whole number between the value of 'minimum' ( 1 ) and 'maximum' ( 100 )
15+ By creating a constant variable called 'num' and assigning it the value of the expression , we can generate a random number within the mentioned range .
16+
17+ When running the program we have to understand how the expression works:
18+
19+ we have to work out the parentheses first , maximum - minimum + 1 , which is equal to 100 - 1 + 1 = 100.
20+ so: Math . floor ( Math . random ( ) * 100 ) + 1
21+ Math . random ( ) produces a pseudo - random decimal number that is greater than or equal to 0 but less than 1.
22+
23+ Next , we need to multiply the result from Math . random ( ) by 100. This may give us the result of a decimal between 0 and 100.
24+ Next step is to use Math . floor ( ) wich then rounds down the decimal number to the nearest whole integer . This then means that the result will be a whole number between 0 and 99.
25+ But we want 1 - 100 , so we add 1 to the result of Math . floor ( ) this then changes the range to be inclusive of 1 and 100.
26+ After adding 1 , we need to round down the number to the nearest interger , this is done by using Math . floor ( ) . After this we will have a whole number between maximum and minimum .
27+ Therefore , the final result of num will be a random whole number between 1 and 100 , inclusive .
0 commit comments