Skip to content

Commit dd4f83e

Browse files
Complete exercise 4 in Sprint 2
1 parent 70cfb4a commit dd4f83e

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

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

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

44
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
5-
5+
console.log(num);
66
// In this exercise, you will need to work out what num represents?
77
// Try breaking down the expression and using documentation to explain what it means
88
// It will help to think about the order in which expressions are evaluated
99
// Try logging the value of num and running the program several times to build an idea of what the program is doing
10+
//
11+
//`num` represents a random integer between 1 and 100 (inclusive).
12+
//
13+
// Breakdown of the expression (from inside out):
14+
// 1. Math.random() returns a decimal between 0 (inclusive) and 1 (exclusive).
15+
// 2. (maximum - minimum + 1) = (100 - 1 + 1) = 100, the size of the range.
16+
// 3. Multiplying them gives a decimal between 0 and 100 (not including 100).
17+
// 4. Math.floor() rounds it down to a whole number between 0 and 99.
18+
// 5. Adding `minimum` (1) shifts the range so the result is between 1 and 100.
19+
20+

0 commit comments

Comments
 (0)