You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 6ac46a6
Browse filesBrowse the repository at this point in the historyBrowse files
// 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
+
10
7
11
8
//Solution
12
9
//- "num" represents a random whole number between 1 and 100.
10
+
13
11
//Break down of expression
14
12
//-Math.random: This generates a random decimal number between 0 and 1. It can return 0, but never reaches 1. (Example: 0.876).
13
+
15
14
//-(maximum - minimum + 1):
16
15
// 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.
17
16
// The +1 is important as without it, we would only reach 99 possibilities, that is 0 - 99.
17
+
18
18
//-Math.random() * (maximum - minimum + 1):
19
19
// 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.
20
20
// Example to justify expression: 0.876 * 100 = 87.6
21
+
21
22
//-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.
22
23
// That is 87.6 becomes 87.
24
+
23
25
// + minimum: This shifts the whole range up by adding 1. So instead of landing on 0 - 99, we have 1 - 100.
0 commit comments