Skip to content

Commit fb5269b

Browse files
feat: add math object
1 parent b2ff4de commit fb5269b

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

part1 (Basics)/07_math.js

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
1-
// INFO: Math is JavaScript
1+
// INFO: Math in JavaScript
2+
3+
// NOTE: Math Object Operators and Methods
24

3-
// NOTE: Mathematical Operators
45
/*
5-
1. Addition Operator: +
6-
2. Subtraction Operator: -
7-
3. Multiplication Operator: *
8-
4. Division Operator: /
9-
5. Remainder Operator: %
10-
6. Exponentiation Operator: **
6+
Math Operators:
7+
1. Exponentiation: **
8+
(Alternative: Math.pow)
9+
10+
Math Object Properties:
11+
- Math.PI → Returns the value of π
12+
13+
Math Object Methods:
14+
- Math.sqrt(x) → Square root of x
15+
- Math.floor(x) → Rounds down to nearest integer
16+
- Math.ceil(x) → Rounds up to nearest integer
17+
- Math.round(x) → Rounds to nearest integer
18+
- Math.random() → Returns a random number between 0 and 1
1119
*/
1220

13-
console.log(1 + 1);
14-
console.log(2 - 2);
15-
console.log(2 * 6);
16-
console.log(2 / 6);
17-
console.log(2 % 4);
18-
19-
// this two works same but the recommendation is the first one
20-
console.log(2 ** 4);
21-
console.log(Math.pow(2, 4));
22-
23-
// Properties of Math Object
24-
console.log(Math.PI);
25-
26-
// Methods of Math Object
27-
console.log(Math.sqrt(16)); // 4
28-
console.log(Math.floor(2.99)); // 2
29-
console.log(Math.floor(-2.5)); // -3
30-
console.log(Math.ceil(2.1)); // 3
31-
console.log(Math.ceil(-2.5)); // -2
32-
console.log(Math.round(2.5)); // 3
33-
console.log(Math.round(2.4)) // 2
34-
console.log((Math.random())); // any random value
35-
console.log(Math.round(Math.random() * 20)); // gives the random value 1 to 20
21+
// Exponentiation (Preferred way)
22+
console.log(2 ** 4); // Output: 16
23+
24+
// Exponentiation (Alternative)
25+
console.log(Math.pow(2, 4)); // Output: 16
26+
27+
// Math Property
28+
console.log(Math.PI); // Output: 3.141592653589793
29+
30+
// Math Methods
31+
console.log(Math.sqrt(16)); // Output: 4
32+
33+
console.log(Math.floor(2.99)); // Output: 2
34+
console.log(Math.floor(-2.5)); // Output: -3
35+
36+
console.log(Math.ceil(2.1)); // Output: 3
37+
console.log(Math.ceil(-2.5)); // Output: -2
38+
39+
console.log(Math.round(2.5)); // Output: 3
40+
console.log(Math.round(2.4)); // Output: 2
41+
42+
console.log(Math.random()); // Output: Random value between 0 and 1
43+
console.log(Math.round(Math.random() * 20)); // Output: Random integer from 0 to 20

0 commit comments

Comments
 (0)