Skip to content

Commit ba118b5

Browse files
committed
Add explanatory comments to random number generator code
1 parent c77a3cf commit ba118b5

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
@@ -2,8 +2,16 @@ const minimum = 1;
22
const maximum = 100;
33

44
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
5+
console.log(num);
56

67
// In this exercise, you will need to work out what num represents?
78
// Try breaking down the expression and using documentation to explain what it means
89
// It will help to think about the order in which expressions are evaluated
910
// Try logging the value of num and running the program several times to build an idea of what the program is doing
11+
12+
// Math.random() returns a random decimal number between 0 and 1
13+
// (maximum - minimum + 1) represents the size of range of numbers we want to include. We add +1 because both minim and maximum are included in the range
14+
// Math.random() * (maximum - minimum + 1) gives us a random decimal between 0 and 100, but still decimal
15+
// Math.floor() rounds down to the nearest whole number. So this. turns decimals like 99.9 into 99
16+
// + minimum since minimum = 1, this shifts the entire random range up by 1. so instead of 0-100, the range becomes 1-100
17+
// num should return a random integer between 1 and 100

0 commit comments

Comments
 (0)