Çıktı Değeri:
0
Bilgisayar biliminde veri boyutları her zaman 1024 (2¹⁰) ile çarpılır veya bölünür:
Her birim, bir öncekinin 1024 katıdır. Bu yüzden üstel fonksiyon kullanırız:
const byteValues = ['Byte', 'KiloByte', 'MegaByte', 'GigaByte', 'TeraByte'];
// Index: 0 1 2 3 4
// Çarpan: 1 1024 1024² 1024³ 1024⁴
Ana Fikir: Birimler arası fark, üstel çarpanı belirler
function calculate() {
// Kaynak ve hedef birimlerin index farkını bul
const difference = from.selectedIndex - to.selectedIndex;
// Fark pozitifse (büyükten küçüğe): çarp
// Fark negatifse (küçükten büyüğe): böl (çarpmanın tersi)
const result = data.value * Math.pow(1024, difference);
return result;
}
Örnek 1: MB → KB (Büyükten Küçüğe)
// 5 MB → KB
// from.selectedIndex = 2 (MB)
// to.selectedIndex = 1 (KB)
// difference = 2 - 1 = 1
// 5 × 1024¹ = 5 × 1024 = 5120 KB
Örnek 2: KB → GB (Küçükten Büyüğe)
// 1048576 KB → GB
// from.selectedIndex = 1 (KB)
// to.selectedIndex = 3 (GB)
// difference = 1 - 3 = -2
// 1048576 × 1024⁻² = 1048576 / (1024 × 1024) = 1 GB
Örnek 3: Aynı Birim (Fark = 0)
// 5 MB → MB
// difference = 0
// 5 × 1024⁰ = 5 × 1 = 5 MB
Math.pow(1024, difference) fonksiyonu 1024'ü "difference" üssüne yükseltir:
Math.pow(1024, 0) // 1
Math.pow(1024, 1) // 1024
Math.pow(1024, 2) // 1,048,576
Math.pow(1024, 3) // 1,073,741,824
Math.pow(1024, -1) // 0.0009765625 (1/1024)
Math.pow(1024, -2) // 0.000000953674316 (1/1048576)
Kullanıcının sadece sayı ve ondalık nokta girmesine izin verilir:
// Regex: /^\d*\.?\d*$/
// ^ → Başlangıç
// \d* → 0 veya daha fazla rakam
// \.? → 0 veya 1 ondalık nokta
// \d* → 0 veya daha fazla rakam
// $ → Son
// Geçerli: "5", "10.5", "123.456"
// Geçersiz: "abc", "5.5.5", "-10"