Skip to content

Commit 921ba5a

Browse files
committed
Refactor random number generation logic and enhance comments for clarity (exercise 4-random.js)
1 parent aa951b4 commit 921ba5a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,31 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
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
8+
9+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
10+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
11+
//
12+
13+
//Mach.random() - generates a random number between 0 (inclusive) and 1 (exclusive):
14+
const num1=Math.random();
15+
console.log(num1);
16+
17+
const num3=(maximum - minimum + 1);
18+
//(maximum - minimum + 1) - calculates the range of numbers we want (in this case, 100 - 1 + 1 = 100)
19+
//so by adding this to our random number we are scaling it to be between 0 and 100
20+
console.log(num3);
21+
const num2=Math.random()*(maximum - minimum + 1);
22+
console.log(num2);
23+
//until this point we have a random number between 0 (inclusive) and 100 (exclusive)but is can be a decimal number
24+
// Math.floor() - rounds down to the nearest whole number, so we now have a random integer between 0 and 99
25+
26+
const num5 = Math.floor(Math.random() * (maximum - minimum + 1)) ;
27+
console.log(`it suppose to be a rounded number from 0-99 ${num5}`);
28+
29+
//finally By adding 1, the number will be between 1 and 100, including both.
30+
31+
console.log(`it suppose to be a rounded number from 1-100 ${num}`);
32+
833
// It will help to think about the order in which expressions are evaluated
934
// Try logging the value of num and running the program several times to build an idea of what the program is doing
35+

0 commit comments

Comments
 (0)