Skip to content

Commit 76e3f65

Browse files
committed
Enhance comments to clarify the random number generation process in 4-random.js
1 parent 2b57ffd commit 76e3f65

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,31 @@ const maximum = 100;
44
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
55

66
// In this exercise, you will need to work out what num represents?
7+
8+
//After running this exercise a few times I can see that 'num' is a random number between 1 and 100
9+
10+
console.log(num);
11+
712
// Try breaking down the expression and using documentation to explain what it means
13+
14+
// at the top we can see our minimum and maximum values which are fixed therefore Constants,
15+
// we then have our variable num which is assigned a value using the Math object and its methods floor and random
16+
// Math.random() generates a random decimal number between 0 (inclusive) and 1 (exclusive)
17+
// We then multiply this random number by the range of our desired numbers which is (maximum - minimum + 1)
18+
819
// It will help to think about the order in which expressions are evaluated
20+
21+
//the same as math, multiplication is done before addition and subtraction
22+
// so we first calculate (maximum - minimum + 1) which is (100 - 1 + 1) = 100
23+
// then we multiply the random decimal number by 100 which gives us a number between 0 and 100 (but not including 100)
24+
// then we apply Math.floor() to round down to the nearest whole number, giving us a number between 0 and 99
25+
// finally we add the minimum value (1) to shift the range up, resulting in a final value between 1 and 100 (inclusive)
26+
27+
// So in summary, 'num' is a random integer between 1 and 100, inclusive of both endpoints.
28+
29+
930
// Try logging the value of num and running the program several times to build an idea of what the program is doing
31+
32+
//you can see how random function generates different numbers each time the program is run
33+
//and how the range is always between 1 and 100
34+

0 commit comments

Comments
 (0)