Skip to content

Commit 3af2171

Browse files
docs: break down random number expression and evaluation order
1 parent 0daa315 commit 3af2171

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

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
console.log(num);
66
// In this exercise, you will need to work out what num represents?
7+
// num represents random whole number between 1 to 100
8+
//It is generated by:
9+
//Creating a random decimal between 0 and 1 using Math.random()
10+
//Scaling it to the range 1 to 100
11+
//Rounding it down using Math.floor() so it becomes an integer
12+
713
// Try breaking down the expression and using documentation to explain what it means
814
// It will help to think about the order in which expressions are evaluated
915
// Try logging the value of num and running the program several times to build an idea of what the program is doing
16+
// Math.random() produces a random decimal number between 0 (inclusive) and 1 (exclusive). example : .10,.23 etc
17+
//it will never return 1
18+
// scale the random decimal number using multiplication with 100(maximum-minimum+1)
19+
//Math.floor() this method will remove the decimal part and return the nearest integer from 0 to 99
20+
// +minimum will shift the range from 0 to 99 to 1 to 100.

0 commit comments

Comments
 (0)