Skip to content

Commit e2d0541

Browse files
committed
docs: explain random integer generation formula
1 parent 38663ff commit e2d0541

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

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+
/*
12+
What 'num' represents: A random whole integer between 1 and 100 inclusive.
13+
14+
Order of evaluation breakdown:
15+
1. (maximum - minimum + 1) // Calculates total size of target range (100).
16+
2. Math.random() //Generates a random decimal between 0 and 0.999...
17+
3. Math.random() * 100 // Scales decimal up to a range between 0 and 99.999...
18+
4. Math.floor(...) // Rounds down to nearest integer, yielding 0 to 99.
19+
5. + minimum //Shifts final range up by 1, resulting in 1 to 100.
20+
*/
21+
22+
console.log(num);

0 commit comments

Comments
 (0)