File tree Expand file tree Collapse file tree 1 file changed +12
-1
lines changed
Expand file tree Collapse file tree 1 file changed +12
-1
lines changed Original file line number Diff line number Diff line change 11const minimum = 1 ;
22const maximum = 100 ;
33
4- const num = Math . floor ( Math . random ( ) * ( maximum - minimum + 1 ) ) + minimum ;
4+ const num = Math . floor ( Math . random ( ) * ( maximum - minimum + 1 ) ) + minimum ; // Generates a random whole number between minimum and maximum
55
66// In this exercise, you will need to work out what num represents?
77// Try breaking down the expression and using documentation to explain what it means
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
10+
11+ // Generate a random decimal number between 0 and 1
12+ const decimal = Math . random ( ) ;
13+ // Calculate the range size
14+ const range = maximum - minimum + 1 ;
15+ // Scale the decimal to the range
16+ const scaled = decimal * range ;
17+ // Round down to get a whole number
18+ const floored = Math . floor ( scaled ) ;
19+ // Shift the number up to start at the minimum
20+ const result = floored + minimum ;
You can’t perform that action at this time.
0 commit comments