Skip to content

Commit 2c9afad

Browse files
committed
Explain the expression assigned to const num
1 parent d845bba commit 2c9afad

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,32 @@ const minimum = 1;
22
const maximum = 100;
33

44
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
5-
5+
console.log(num);
66
// In this exercise, you will need to work out what num represents?
77
// Try breaking down the expression and using documentation to explain what it means
88
// It will help to think about the order in which expressions are evaluated
99
// Try logging the value of num and running the program several times to build an idea of what the program is doing
10+
11+
//Answers
12+
// num is a constant variable that stores a random integer between the minimum and maximum values (inclusive), generated using Math.random() and Math.floor()
13+
14+
15+
16+
// Math.random()
17+
// This expression gives you a random decimal number between 0 and 1.
18+
// It could be something like 0.25 or 0.879, but it will never reach 1.
19+
20+
// (maximum - minimum + 1)
21+
// This expression calculates how many numbers are in the range you want.
22+
// For example, if your range is 1 to 100, the result is 100 because there are 100 possible values.
23+
24+
// Math.random() * (maximum - minimum + 1)
25+
// This expression multiplies the random decimal by your range.
26+
27+
28+
// Math.floor(...)
29+
// This expression rounds the number down to the nearest integer.
30+
// For example, 37.6 becomes 37, and 99.9 becomes 99.
31+
32+
// + minimum
33+
// Variable minimum is added to the expression "Math.floor(Math.random() * (maximum - minimum + 1))" and the the value is stored inside the variable num

0 commit comments

Comments
 (0)