Skip to content

Commit 428477b

Browse files
committed
Add 08_Math.js
1 parent 123fb7f commit 428477b

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

08_Math.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
console.log(Math); // Object [Math] {}
2+
3+
4+
5+
// ------------------------------------------------------------------------------------------------
6+
7+
8+
9+
// Math Properties :-
10+
11+
// .PI - return the value of PI
12+
console.log(Math.PI); // 3.141592653589793
13+
14+
15+
16+
// ------------------------------------------------------------------------------------------------
17+
18+
19+
20+
// Math Methods :-
21+
22+
// .abs() - return the absolute (positive) value
23+
console.log(Math.abs(3)); // 3
24+
console.log(Math.abs(-3)); // 3
25+
26+
// .sign() - return +1, -1 & 0 based on if the number is positive, negative or zero
27+
console.log(Math.sign(3)); // 1
28+
console.log(Math.sign(-3)); // -1
29+
console.log(Math.sign(0)); // 0
30+
31+
// .pow() - return the value of second number to be the power of first number
32+
console.log(Math.pow(2, 3)); // 8
33+
console.log(Math.pow(3, 2)); // 9
34+
35+
// .sqrt() - return the square root
36+
console.log(Math.sqrt(4)); // 2
37+
console.log(Math.sqrt(9)); // 3
38+
39+
// .round() - round off to the nearest integer
40+
console.log(Math.round(2.4)); // 2 (less than 5)
41+
console.log(Math.round(2.5)); // 3 (equal to 5)
42+
console.log(Math.round(2.6)); // 3 (greater than 5)
43+
44+
// .ceil() - round up to the nearest integer, i.e., if +1 after ".", increase the number by +1, else return number itself
45+
console.log(Math.ceil(2)); // 2
46+
console.log(Math.ceil(2.0)); // 2
47+
console.log(Math.ceil(2.1)); // 3
48+
49+
// .floor() - round down to the nearest integer, i.e., if "." exists, remove it)
50+
console.log(Math.floor(2)); // 2
51+
console.log(Math.floor(2.0)); // 2
52+
console.log(Math.floor(2.1)); // 2
53+
54+
// .max() - return the maximum vallue in a set of numbers
55+
console.log(Math.max(1, 2, 3, 4, 5)); // 5
56+
57+
// .min() - return the minimum vallue in a set of numbers
58+
console.log(Math.min(1, 2, 3, 4, 5)); // 1
59+
60+
// .random() - return a random number between 0 (inclusive) and 1 (exclusive)
61+
console.log(Math.random()); // 0.4234567890123456
62+
console.log(Math.random() * 10); // 4.234567890123456
63+
console.log(Math.floor(Math.random() * 10) + 1); // 1 to 10 (random integer)

0 commit comments

Comments
 (0)