Skip to content

Commit 1c71d92

Browse files
committed
complete exercise 4-random
1 parent 92282ff commit 1c71d92

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

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

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

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

6+
console.log(num);
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+
// Maths operations are solved using the PEMDAS order: parentheses, exponents, multiplication & division and addition & subtraction. Javascript does the same
13+
// The program starts by executing Math.random(), which is a method that returns a float number (decimal, not integer) that is greater or equal to 0 and smaller than 1. This number is random and will always be different, but for my explanation I will use 0.123456
14+
// Next the program executes the other parenthesis, by first adding 1 to minimum and then subtracting the result from maximum. In this case it would be -1 + 1 = 0 and then 100 - 0 = 100
15+
// The program will then move on to multiply 0.123456 x 100 = 12.3456
16+
// The next step is Math.floor(), which is a method that rounds a number down to it's closest integer. In our case 12.3456 becomes 12
17+
// Lastly the program executes the last addition, Math.floor() + minimum, and assigns the result to num: num = 12 + 1 = 13
18+
// The value of num is therefore a random number between 1 and 100
19+
// Additional note:
20+
// Adding minimum at the end is necessary to avoid the final result being 0, which could happen if, our Math.random() result was something like 0.000123
21+
// 0.000123 x 100 = 0.0123, which would then be round down by Math.floor() to 0

0 commit comments

Comments
 (0)