Skip to content

Commit 6ac46a6

Browse files
committed
Refactor comments in 4-random.js for readability
1 parent 57981df commit 6ac46a6

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@ const maximum = 100;
33

44
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
55
console.log(num);
6-
// In this exercise, you will need to work out what num represents?
7-
// Try breaking down the expression and using documentation to explain what it means
8-
// It will help to think about the order in which expressions are evaluated
9-
// Try logging the value of num and running the program several times to build an idea of what the program is doing
6+
107

118
//Solution
129
//- "num" represents a random whole number between 1 and 100.
10+
1311
//Break down of expression
1412
//-Math.random: This generates a random decimal number between 0 and 1. It can return 0, but never reaches 1. (Example: 0.876).
13+
1514
//-(maximum - minimum + 1):
1615
// This expression can be simplified as (100 - 1 + 1), which equals 100. This tells the program how many possible whole numbers there are. There are 100 possibilities.
1716
// The +1 is important as without it, we would only reach 99 possibilities, that is 0 - 99.
17+
1818
//-Math.random() * (maximum - minimum + 1):
1919
// This function multiplies the random decimal by the range size. Since Math.random() represents numbers >=0 and <1, this expression produces value anywhere from 0 up to 100, excluding 100.
2020
// Example to justify expression: 0.876 * 100 = 87.6
21+
2122
//-Math.floor: This static method rounds down to the nearest whole number, discarding the decimal. This turns the continuous range into whole numbers, from 0 - 99.
2223
// That is 87.6 becomes 87.
24+
2325
// + minimum: This shifts the whole range up by adding 1. So instead of landing on 0 - 99, we have 1 - 100.

0 commit comments

Comments
 (0)