File tree Expand file tree Collapse file tree 1 file changed +13
-0
lines changed
Expand file tree Collapse file tree 1 file changed +13
-0
lines changed Original file line number Diff line number Diff line change 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+ */
112const minimum = 1 ;
213const 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 ) ;
You can’t perform that action at this time.
0 commit comments