Number & Math μ€μ΅ μμ
1. μ
λ ₯κ°μ΄ μ«μμΈμ§ νλ³ (isNaN, ifλ¬Έ)
// [1] μ
λ ₯κ°μ΄ μ«μμΈμ§ νλ³ (isNaN, ifλ¬Έ)
const value = '123abc';
if (isNaN(value)) {
console.log('μ«μκ° μλλλ€');
} else {
console.log('μ«μμ
λλ€');
}
2. μ λκ°μ΄ 10 μ΄μμΈμ§ νλ³ (Math.abs, μΌνμ°μ°μ)
// [2] μ λκ°μ΄ 10 μ΄μμΈμ§ νλ³ (Math.abs, μΌνμ°μ°μ)
const num = -15;
const result = Math.abs(num) >= 10 ? '10 μ΄μ' : '10 λ―Έλ§';
console.log(result);
3. λ°μ¬λ¦Όνμ¬ μ§μ/νμ νλ³
// [3] λ°μ¬λ¦Όνμ¬ μ§μ/νμ νλ³
const num = 4.6;
const rounded = Math.round(num);
console.log(rounded % 2 === 0 ? 'μ§μ' : 'νμ');
4. 1~100 μ¬μ΄ λμ μμ± ν 50 μ΄μ/λ―Έλ§ νλ³
// [4] 1~100 μ¬μ΄ λμ μμ± ν 50 μ΄μ/λ―Έλ§ νλ³
const rand = Math.floor(Math.random() * 100) + 1;
console.log(rand, rand >= 50 ? '50 μ΄μ' : '50 λ―Έλ§');
5. μ
λ ₯κ°μ΄ μ μμΈμ§ νλ³(Number.isInteger)
// [5] μ
λ ₯κ°μ΄ μ μμΈμ§ νλ³(Number.isInteger)
const value = 3.14;
console.log(Number.isInteger(value) ? 'μ μ' : 'μ€μ');
6. λ μ μ€ ν° κ°/μμ κ° κ΅¬νκΈ° (Math.max/Math.min, μΌνμ°μ°μ)
// [6] λ μ μ€ ν° κ°/μμ κ° κ΅¬νκΈ° (Math.max/Math.min, μΌνμ°μ°μ)
const a = 7, b = 12;
console.log('λ ν° κ°:', a > b ? a : b);
console.log('λ μμ κ°:', Math.min(a, b));
7. λ°μ¬λ¦Όν κ°μ΄ 10μ λ°°μμΈμ§ νλ³
// [7] λ°μ¬λ¦Όν κ°μ΄ 10μ λ°°μμΈμ§ νλ³
const num = 47.8;
const rounded = Math.round(num / 10) * 10;
console.log(rounded % 10 === 0 ? '10μ λ°°μ' : '10μ λ°°μ μλ');
8. μ€μν: μν κ°κ²© μμμ μ΄ν μ μ(λ΄λ¦Ό)
// [8] μ€μν: μν κ°κ²© μμμ μ΄ν μ μ(λ΄λ¦Ό)
const price = 1999.99;
console.log('κ²°μ κΈμ‘:', Math.floor(price) + 'μ');
9. μ€μν: BMI κ³μ°(μμμ 2μ리, toFixed)
// [9] μ€μν: BMI κ³μ°(μμμ 2μ리, toFixed)
const height = 1.7; // m
const weight = 65; // kg
const bmi = weight / (height * height);
console.log('BMI:', bmi.toFixed(2));
10. μ€μν: λλ€ OTP(6μ리) μμ±
// [10] μ€μν: λλ€ OTP(6μ리) μμ±
const otp = Math.floor(100000 + Math.random() * 900000);
console.log('OTP:', otp);
11. μ€μν: κ±°μ€λ¦λ κ³μ° (Math.floor)
// [11] μ€μν: κ±°μ€λ¦λ κ³μ° (Math.floor)
const total = 10000;
const price = 3700;
const change = total - price;
const coin = 500;
console.log('500μ λμ :', Math.floor(change / coin) + 'κ°');
12. μ€μν: λ°μ¬λ¦Όνμ¬ νμ κ³μ°
// [12] μ€μν: λ°μ¬λ¦Όνμ¬ νμ κ³μ°
const score = 86.7;
const grade = Math.round(score / 10);
console.log('νμ :', grade >= 9 ? 'A' : grade >= 8 ? 'B' : grade >= 7 ? 'C' : 'D');
13. μ€μν: 2μ§μ/16μ§μ λ³ν
// [13] μ€μν: 2μ§μ/16μ§μ λ³ν
const num = 255;
console.log('2μ§μ:', num.toString(2));
console.log('16μ§μ:', num.toString(16));
14. μ€μν: μμμ μ΄ν λ²λ¦Ό(μλ κ³μ°)
// [14] μ€μν: μμμ μ΄ν λ²λ¦Ό(μλ κ³μ°)
const price = 1234.56;
console.log('μλ:', Math.trunc(price));
15. μ€μν: μ κ³±κ·Ό, κ±°λμ κ³± νμ©
// [15] μ€μν: μ κ³±κ·Ό, κ±°λμ κ³± νμ©
const a = 9;
const b = 2;
console.log('μ κ³±κ·Ό:', Math.sqrt(a));
console.log('2μ 5μ κ³±:', Math.pow(2, 5));