Skip to content

Commit 906ab34

Browse files
included notes to work out what num represents; by breaking down the expression
1 parent fca74e9 commit 906ab34

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,32 @@ const maximum = 100;
33

44
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
55

6+
console.log(num);
7+
68
// In this exercise, you will need to work out what num represents?
79
// Try breaking down the expression and using documentation to explain what it means
810
// It will help to think about the order in which expressions are evaluated
911
// Try logging the value of num and running the program several times to build an idea of what the program is doing
12+
13+
// when first evaluating the full num expression i got the answer 99
14+
// The second time i got 50 (which means .floor and .random are doing something to change the value of the expression each run)
15+
16+
// The order of operations is as follows:
17+
18+
// 1. The expression inside the parentheses is evaluated first: (maximum - minimum + 1) which gives us 100 - 1 + 1 = 100
19+
// 2. Then Math.random() is called, which generates a random decimal number between 0 and 1.
20+
// 3. The result of Math.random() is multiplied by the result of step 1, giving us a random decimal number between 0 and 100.
21+
// 4. Math.floor() is then called on the result of step 3, which rounds it down to the nearest whole number, giving us a random integer between 0 and 99.
22+
// 5. Finally, the minimum value (1) is added to the result of step 4, giving us a random integer between 1 and 100.
23+
24+
// When num = 99
25+
// math.random gives us 0.98 (or something close to it)
26+
// 0.98 * 100 = 98
27+
// math.floor(98) = 98
28+
// 98 + 1 = 99
29+
30+
// When num = 50
31+
// math.random gives us 0.49 (or something close to it)
32+
// 0.49 * 100 = 49
33+
// math.floor(49) = 49
34+
// 49 + 1 = 50

0 commit comments

Comments
 (0)