Skip to content

Commit 49913e8

Browse files
committed
4-random.js
1 parent 9ae7180 commit 49913e8

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
const minimum = 1;
22
const 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;
5+
console.log(num);
6+
57

68
// In this exercise, you will need to work out what num represents?
79
// Try breaking down the expression and using documentation to explain what it means
810
// It will help to think about the order in which expressions are evaluated
911
// Try logging the value of num and running the program several times to build an idea of what the program is doing
12+
13+
// Breaking down what num represents:
14+
// num generates a random integer between 1 and 100 (inclusive)
15+
16+
// Step-by-step evaluation:
17+
// 1. Math.random() → generates a decimal between 0 (inclusive) and 1 (exclusive)
18+
// 2. (maximum - minimum + 1) → calculates the range size: (100 - 1 + 1) = 100
19+
// 3. Math.random() * 100 → gives a decimal between 0 and 99.999...
20+
// 4. Math.floor(...) → rounds down to get integers from 0 to 99
21+
// 5. + minimum → shifts the range from 0-99 to 1-100
22+
23+
// So num represents a random integer from 1 to 100

0 commit comments

Comments
 (0)