Skip to content

Commit 938fa67

Browse files
committed
Answering question in 4-random.js
1 parent e161bfe commit 938fa67

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,20 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
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+
console.log(num);
12+
13+
// Answer: num is a random number between 1 and 100, including 1 and 100. So if I change the values of minimum
14+
// and maximum it will be a random value between them, including them.
15+
16+
// Math.random generates a random value between 0 and 1, including 0 but not 1.
17+
18+
// (maximum - minimum + 1) gives the amounts of values we want to be able to generate.
19+
// If minimum was 60 and maximum was 70, inclusive, that would make 11 possible values
20+
21+
// Math.random * (maximum - minimum + 1) would generate any value between 0 and 11, including 0 but not 11
22+
23+
// Math.floor rounds the value down, leaving us with any value between 0 and 10 inclusive
24+
25+
// + minimum at the end, 60 in our thought experiment, makes the values range between 60 and 70 inclusive
26+
// which is what we wanted.

0 commit comments

Comments
 (0)