Skip to content

Commit a102624

Browse files
committed
run the program several times to observe the results
1 parent dd9a1ca commit a102624

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,20 @@ const minimum = 1;
22
const maximum = 100;
33

44
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
5+
console.log(num);
56

67
// In this exercise, you will need to work out what num represents?
8+
// num stores the result of the expression after calculating it.
79
// Try breaking down the expression and using documentation to explain what it means
8-
// It will help to think about the order in which expressions are evaluated
9-
// Try logging the value of num and running the program several times to build an idea of what the program is doing
10+
// There is a range of numbers from 1 to 100. In the expression,
11+
// (maximum - minimum + 1) calculates the size of the possible integers, which is 100.
12+
// The Math.random() function generates a random decimal number in the range 0 ≤ x < 1.
13+
// This random decimal is then multiplied by the range size to scale it to a
14+
// value between 0 and 100. The result is a floating-point number in this interval.
15+
// Next, Math.floor() is used to round the number down to the largest integer less than
16+
// or equal to the calculated value, producing an integer between 0 and 99.
17+
// Finally, by adding minimum, the range is shifted from 0–99 to 1–100, giving a random
18+
// integer within the required range.
19+
// I have run 5 times the program and this produces different values each time because
20+
// Math.random() generates a new random decimal number every time.
21+
// Since the final value of num depends on this function, the result changes on each execution.

0 commit comments

Comments
 (0)