Skip to content

Commit ca79bc0

Browse files
authored
exercise 4 complete
1 parent e9bd9bb commit ca79bc0

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
const minimum = 1;
1+
// A program to generate a random number between 1 and 100
22
const maximum = 100;
33

4-
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
4+
const num = Math.floor(Math.random() * (maximum)) + 1;
55

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+
console.log(num);
12+
13+
//Math.random generates a decimal number between 0 and 1
14+
//multiplying (maximum - minimum + 1) this part generates a number between 0 and 100 that can still have decimal places.
15+
/// but since minimum in this example is 1 it can be simplified from (maximum -1 +1) ==>(maximum)
16+
//Math.floor rounds down to the nearest whole number
17+
//+ minimum shifts the range from 0-99 to 1-100
18+
//so the final output is a whole number between 1 and 100, this could also be simplified to +1
19+
//obviously the function was more useful for different ranges when using the minimum expression
20+
/// without my simplification, but it is more concise for this specific example.
21+

0 commit comments

Comments
 (0)