|
1 | | -// INFO: Math in JavaScript |
| 1 | +// math.js |
2 | 2 |
|
3 | | -// NOTE: Math Object Operators and Methods |
| 3 | +// INFO: Math in JavaScript |
4 | 4 |
|
5 | 5 | /* |
| 6 | +The Math object in JavaScript provides properties and methods for mathematical constants and functions. |
| 7 | +
|
| 8 | +--- |
| 9 | +
|
6 | 10 | Math Operators: |
7 | 11 | 1. Exponentiation: ** |
8 | | - (Alternative: Math.pow) |
| 12 | + - Raises a number to the power of another. |
| 13 | + - Alternative: Math.pow(base, exponent) |
| 14 | +
|
| 15 | +--- |
9 | 16 |
|
10 | 17 | Math Object Properties: |
11 | | -- Math.PI → Returns the value of π |
| 18 | +- Math.PI |
| 19 | + - The constant π (3.141592653589793), useful in geometry. |
| 20 | +
|
| 21 | +--- |
12 | 22 |
|
13 | 23 | 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 |
| 24 | +
|
| 25 | +1. Math.sqrt(x) |
| 26 | + - Returns the square root of x. |
| 27 | + - Example: Square root of 16 is 4. |
| 28 | +
|
| 29 | +2. Math.floor(x) |
| 30 | + - Rounds a number down to the nearest integer. |
| 31 | + - Works for positive and negative numbers. |
| 32 | +
|
| 33 | +3. Math.ceil(x) |
| 34 | + - Rounds a number up to the nearest integer. |
| 35 | + - Works for positive and negative numbers. |
| 36 | +
|
| 37 | +4. Math.round(x) |
| 38 | + - Rounds a number to the nearest integer. |
| 39 | + - Rounds .5 and above up, below .5 down. |
| 40 | +
|
| 41 | +5. Math.random() |
| 42 | + - Returns a random float between 0 (inclusive) and 1 (exclusive). |
| 43 | +
|
| 44 | +6. Math.abs(x) |
| 45 | + - Returns the absolute value of x. |
| 46 | +
|
| 47 | +7. Math.min(...values) |
| 48 | + - Returns the smallest value among the provided numbers. |
| 49 | +
|
| 50 | +8. Math.max(...values) |
| 51 | + - Returns the largest value among the provided numbers. |
| 52 | +
|
| 53 | +9. Math.log(x) |
| 54 | + - Returns the natural logarithm (base e) of x. |
| 55 | +
|
| 56 | +10. Math.exp(x) |
| 57 | + - Returns e raised to the power of x. |
| 58 | +
|
| 59 | +11. Math.trunc(x) |
| 60 | + - Returns the integer part of a number by removing any fractional digits. |
| 61 | +
|
19 | 62 | */ |
20 | 63 |
|
21 | | -// Exponentiation (Preferred way) |
22 | | -console.log(2 ** 4); // Output: 16 |
| 64 | +// --- Exponentiation |
| 65 | + |
| 66 | +console.log(2 ** 4); // 16 |
| 67 | +console.log(Math.pow(2, 4)); // 16 |
| 68 | + |
| 69 | +// --- Math.PI |
| 70 | + |
| 71 | +console.log(Math.PI); // 3.141592653589793 |
| 72 | +// Example: Calculate circumference of a circle with radius 5 |
| 73 | +const radius = 5; |
| 74 | +const circumference = 2 * Math.PI * radius; |
| 75 | +console.log(circumference); // ≈ 31.4159 |
| 76 | + |
| 77 | +// --- Math.sqrt() |
| 78 | + |
| 79 | +console.log(Math.sqrt(16)); // 4 |
| 80 | +console.log(Math.sqrt(2)); // ~1.414 |
| 81 | + |
| 82 | +// --- Math.floor() |
| 83 | + |
| 84 | +console.log(Math.floor(2.99)); // 2 |
| 85 | +console.log(Math.floor(-2.5)); // -3 |
| 86 | + |
| 87 | +// Example: Round down prices to nearest dollar |
| 88 | +let price = 19.99; |
| 89 | +console.log(Math.floor(price)); // 19 |
| 90 | + |
| 91 | +// --- Math.ceil() |
| 92 | + |
| 93 | +console.log(Math.ceil(2.1)); // 3 |
| 94 | +console.log(Math.ceil(-2.5)); // -2 |
| 95 | + |
| 96 | +// Example: Round up for minimum number of tickets to buy |
| 97 | +let ticketsNeeded = 4.2; |
| 98 | +console.log(Math.ceil(ticketsNeeded)); // 5 |
| 99 | + |
| 100 | +// --- Math.round() |
| 101 | + |
| 102 | +console.log(Math.round(2.5)); // 3 |
| 103 | +console.log(Math.round(2.4)); // 2 |
| 104 | +console.log(Math.round(-2.5)); // -2 |
| 105 | + |
| 106 | +// Example: Round a score to nearest integer |
| 107 | +let score = 87.6; |
| 108 | +console.log(Math.round(score)); // 88 |
| 109 | + |
| 110 | +// --- Math.random() |
| 111 | + |
| 112 | +console.log(Math.random()); // Random float between 0 and 1 |
| 113 | + |
| 114 | +// Example: Generate random integer between min and max (inclusive) |
| 115 | +function getRandomInt(min, max) { |
| 116 | + return Math.floor(Math.random() * (max - min + 1)) + min; |
| 117 | +} |
| 118 | +console.log(getRandomInt(1, 10)); // Random integer between 1 and 10 |
| 119 | + |
| 120 | +// --- Math.abs() |
| 121 | + |
| 122 | +console.log(Math.abs(-7)); // 7 |
| 123 | +console.log(Math.abs(7)); // 7 |
| 124 | + |
| 125 | +// Example: Calculate distance between two numbers |
| 126 | +function distance(a, b) { |
| 127 | + return Math.abs(a - b); |
| 128 | +} |
| 129 | +console.log(distance(5, 9)); // 4 |
| 130 | + |
| 131 | +// --- Math.min() |
| 132 | + |
| 133 | +console.log(Math.min(3, 7, 1, 9)); // 1 |
| 134 | + |
| 135 | +// --- Math.max() |
| 136 | + |
| 137 | +console.log(Math.max(3, 7, 1, 9)); // 9 |
| 138 | + |
| 139 | +// --- Math.log() |
23 | 140 |
|
24 | | -// Exponentiation (Alternative) |
25 | | -console.log(Math.pow(2, 4)); // Output: 16 |
| 141 | +console.log(Math.log(Math.E)); // 1 (natural logarithm of e) |
26 | 142 |
|
27 | | -// Math Property |
28 | | -console.log(Math.PI); // Output: 3.141592653589793 |
| 143 | +// --- Math.exp() |
29 | 144 |
|
30 | | -// Math Methods |
31 | | -console.log(Math.sqrt(16)); // Output: 4 |
| 145 | +console.log(Math.exp(1)); // 2.718281828459045 (e^1) |
32 | 146 |
|
33 | | -console.log(Math.floor(2.99)); // Output: 2 |
34 | | -console.log(Math.floor(-2.5)); // Output: -3 |
| 147 | +// --- Math.trunc() |
35 | 148 |
|
36 | | -console.log(Math.ceil(2.1)); // Output: 3 |
37 | | -console.log(Math.ceil(-2.5)); // Output: -2 |
| 149 | +console.log(Math.trunc(4.9)); // 4 |
| 150 | +console.log(Math.trunc(-4.9)); // -4 |
38 | 151 |
|
39 | | -console.log(Math.round(2.5)); // Output: 3 |
40 | | -console.log(Math.round(2.4)); // Output: 2 |
| 152 | +// Example: Remove decimals from a price |
| 153 | +let cost = 25.99; |
| 154 | +console.log(Math.trunc(cost)); // 25 |
41 | 155 |
|
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