Skip to content

Commit 688f25b

Browse files
committed
Breaking the expression and explaining what they mean
1 parent 3fd21e6 commit 688f25b

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ const maximum = 100;
44
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
55

66
// In this exercise, you will need to work out what num represents?
7+
78
// Try breaking down the expression and using documentation to explain what it means
9+
10+
The expression Math.floor() rounds a number down to the nearest integer.
11+
The expression Math.random() generates a random floating-point number between 0 and 1.
12+
The expression (maximum - minimum + 1) calculates the range of numbers between the minimum and maximum values.
13+
The expression Math.random() * (maximum - minimum + 1) generates a random floating-point number between 0 and the range of numbers.
14+
The expression Math.floor(Math.random() * (maximum - minimum + 1)) rounds that random number down to the nearest integer, resulting in a random integer between 0 and the range of numbers.
15+
Finally, adding minimum to that result shifts the range to be between the minimum and maximum values.
816
// It will help to think about the order in which expressions are evaluated
917
// Try logging the value of num and running the program several times to build an idea of what the program is doing
18+
19+
console.log(num);
20+

0 commit comments

Comments
 (0)