Skip to content

Commit f9efd77

Browse files
committed
explanation added on the expression
1 parent 45fdc2a commit f9efd77

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@ 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+
// num is a variable declared to hold (being assigned) the value of the calculated result of the var minimum, var maximum and the Math.random() method
14+
// This expression can be broken down in to main steps
15+
// 1. the inner parentheses (maximum - minimum + 1) is calculated because that is the inner most expression
16+
// 2. then the Math.random() generates random number between 0 & 1 and multiply with the result of (maximum - minimum + 1)
17+
// 3. then the Math.floor() rounds down the result of (Math.random() * (maximum - minimum + 1)) to the nearest whole number
18+
// 4. finally the minimum is being added to the down rounded whole number and the variable num holds the value
19+
// after running the program the value of num looks like this 51,8,85,8,99,21 random numbers between 1 & 100

0 commit comments

Comments
 (0)