File tree Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -7,4 +7,22 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
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+
1011console . log ( num ) ; // outputs a random integer between 1 and 100 (inclusive)
12+
13+ /*
14+ 1. Math.random()
15+ Returns a floating-point number between 0 (inclusive) and 1 (exclusive).
16+ 2. (maximum - minimum + 1)
17+ Calculates the size of the range (the number of possible values).
18+ 3. Math.random() * (maximum - minimum + 1)
19+ Scales the random number to the desired range.
20+ In this case: Math.random() * (100 - 1 + 1) → Math.random() * 100
21+ So now the result is between 0 and 99.999...
22+ 4. + minimum
23+ Shifts the range up by the minimum value.
24+ So 0–99 becomes 1–100
25+ 5. Math.floor(...)
26+ Rounds the result down to the nearest whole number.
27+ So now we get an integer between 0 and 99 inclusive
28+ */
You can’t perform that action at this time.
0 commit comments