Skip to content

Commit 65e9285

Browse files
committed
4-random.js
1 parent 25632b9 commit 65e9285

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/**
2+
* Generates a random integer between the specified minimum and maximum values, inclusive.
3+
*
4+
* The expression `Math.floor(Math.random() * (maximum - minimum + 1)) + minimum` works as follows:
5+
* 1. `Math.random()` generates a floating-point number in the range [0, 1).
6+
* 2. Multiplying by `(maximum - minimum + 1)` scales this to the range [0, maximum - minimum + 1).
7+
* 3. `Math.floor()` rounds the result down to the nearest integer, giving a value in [0, maximum - minimum].
8+
* 4. Adding `minimum` shifts the range to [minimum, maximum].
9+
*
10+
* As a result, `num` will be a random integer between `minimum` and `maximum`, inclusive.
11+
*/
112
const minimum = 1;
213
const maximum = 100;
314

@@ -7,3 +18,5 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
718
// Try breaking down the expression and using documentation to explain what it means
819
// It will help to think about the order in which expressions are evaluated
920
// Try logging the value of num and running the program several times to build an idea of what the program is doing
21+
22+
console.log(num);

0 commit comments

Comments
 (0)