Skip to content

Commit 7d2f848

Browse files
committed
Update 4-random.js
What does num represent with explanation
1 parent 9f0fe23 commit 7d2f848

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,16 @@ 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+
// Math.random returns a random number between 0 (inclusive) and 1 (exclusive).
12+
// const minimum = 1; const maximum = 100; +1 so, 100 - 1 + 1 = 100
13+
// Math.random() * 100 means the decimal number will be multiplied by 100, so the range of possible values is from 0 to 99.9999...
14+
// Math.floor rounds the number down to the nearest whole number, so the possible values are from 0 to 99.
15+
// Eg. 12.9 becomes 12, 0.3 becomes 0, 99.9 becomes 99.
16+
// Brackets(), Multiply*, Function(Math.floor), Addition+
17+
// 1) Pick a random number between 0 and 1, eg. 0.5
18+
// 2) Multiply that number by 100, eg. 0.5 * 100 = 50
19+
// 3) Round that number down to the nearest whole number, eg. Math.floor(50) = 50
20+
// 4) Add 1 to that number, eg. 50 + 1 = 51
21+
22+
// num represents a random whole number between 1 and 100.

0 commit comments

Comments
 (0)