Skip to content

Commit e5b59c3

Browse files
committed
included explanation of the expression
1 parent 5a27b07 commit e5b59c3

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Sprint-1/1-key-exercises/4-random.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
1011
console.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+
*/

0 commit comments

Comments
 (0)