Skip to content

Commit 63ea100

Browse files
committed
completed 4-random.js and tested it with logging to the console
1 parent 302efee commit 63ea100

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

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

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

44
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
5+
console.log(num)
6+
7+
8+
//In this exercise, you will need to work out what num represents?
9+
//Try breaking down the expression and using documentation to explain what it means
10+
//It will help to think about the order in which expressions are evaluated
11+
//Try logging the value of num and running the program several times to build an idea of what the program is doing
12+
13+
/*
14+
Answer
15+
16+
num is a variable that'll hold the output of the expression in line 4, I have run the code several times and I get a
17+
different number each run.
18+
Math.random returns a random decimal number between 0 (inclusive) and 1 (exclusive).
19+
Math.floor rounds the number to the nearest integer.
20+
This expression is evaluated according to parenthesis and operator precedence, so for this expression it will be as follows:
21+
1. (maximum - minimum + 1)
22+
2. Math.random() is called
23+
3. result from Math.random * result from > maximum - minimum + 1
24+
4. Math.floor is then called with (Math.random * result from > maximum - minimum + 1)
25+
5. result from Math.floor + minimum
26+
*/
527

6-
// In this exercise, you will need to work out what num represents?
7-
// Try breaking down the expression and using documentation to explain what it means
8-
// It will help to think about the order in which expressions are evaluated
9-
// Try logging the value of num and running the program several times to build an idea of what the program is doing

0 commit comments

Comments
 (0)