Skip to content

Commit a9f60fe

Browse files
committed
Add explanation of how num is calculated as a random number between munimum and maximum.
1 parent 1891614 commit a9f60fe

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@ 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+
// The variable "num" stores a random whole number between 1 and 100.
12+
// The expression works as follows:
13+
// 1: Math.random() generates a decimal 0 to 1 (not including 1)
14+
// This decimal is multiplied by (maximum - minimum + 1) which gives a number between 0 and 99.999.
15+
// Math.floor() then rounds this down to a whole number between 0 to 99.
16+
// Adding "minimum" shifts the result so the final number is between 1 to 100.
17+
// for this reason num changes every time when you run the program always gives the number between 1 to 100.

0 commit comments

Comments
 (0)