-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
221 lines (189 loc) · 10.3 KB
/
script.js
File metadata and controls
221 lines (189 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
let currentOperator = null;
// Fungsi utilitas untuk mencari FPB (Faktor Persekutuan Terbesar)
const gcd = (a, b) => b === 0 ? a : gcd(b, a % b);
// Fungsi untuk beralih antara mode Operasi dan Konversi
function switchMode(mode) {
const btnOperasi = document.getElementById('btnOperasi');
const btnKonversi = document.getElementById('btnKonversi');
const modeOperasi = document.getElementById('modeOperasi');
const modeKonversi = document.getElementById('modeKonversi');
// Mengatur tampilan mode dan tombol aktif
const isOperasi = mode === 'operasi';
modeOperasi.classList.toggle('hidden', !isOperasi);
modeKonversi.classList.toggle('hidden', isOperasi);
btnOperasi.classList.toggle('active', isOperasi);
btnKonversi.classList.toggle('active', !isOperasi);
clearResult(); // Membersihkan hasil setiap kali mode diganti
}
// Memperbarui UI berdasarkan jenis operasi pecahan yang dipilih
function updateUI() {
const type = document.getElementById('operationType').value;
const whole1 = document.getElementById('whole1Container');
const whole2 = document.getElementById('whole2Container');
whole1.classList.add('hidden');
whole2.classList.add('hidden');
if (type === 'campuran-biasa') {
whole1.classList.remove('hidden');
} else if (type === 'campuran-campuran') {
whole1.classList.remove('hidden');
whole2.classList.remove('hidden');
}
}
// Memperbarui UI untuk mode konversi
function updateConversionUI() {
const type = document.getElementById('conversionType').value;
const convWhole = document.getElementById('convWholeContainer');
convWhole.classList.toggle('hidden', type !== 'campuran');
}
// Mengatur operator matematika yang akan digunakan
function setOperator(op) {
currentOperator = op;
document.getElementById('operatorDisplay').innerText = op === '*' ? '×' : op === '/' ? '÷' : op;
}
// Membersihkan area hasil dan proses
function clearResult() {
document.getElementById('resultText').innerText = '';
document.getElementById('errorText').innerText = '';
document.getElementById('processText').innerHTML = '';
}
// Mengambil dan memvalidasi nilai dari input pecahan
function getFractionValues(wholeId, numId, denId) {
const wholeEl = document.getElementById(wholeId);
const isVisible = wholeEl && !wholeEl.classList.contains('hidden');
const whole = isVisible ? (parseInt(wholeEl.querySelector('input').value) || 0) : 0;
const num = parseInt(document.getElementById(numId).value);
const den = parseInt(document.getElementById(denId).value);
if (isNaN(num) || isNaN(den)) {
throw new Error("Pembilang dan penyebut harus diisi.");
}
if (den === 0) {
throw new Error("Penyebut tidak boleh nol.");
}
// Mengubah ke pecahan biasa (improper fraction) dan menangani angka negatif
const improperNum = (Math.abs(whole) * den + Math.abs(num)) * (whole < 0 || num < 0 ? -1 : 1);
return { num: improperNum, den, original: { whole, num, den, isMixed: isVisible && whole !== 0 } };
}
// Fungsi utama untuk melakukan kalkulasi
function calculate() {
clearResult();
let processSteps = [];
try {
if (!currentOperator) {
throw new Error("Pilih operator terlebih dahulu (+, -, ×, ÷).");
}
const f1 = getFractionValues('whole1Container', 'num1', 'den1');
const f2 = getFractionValues('whole2Container', 'num2', 'den2');
// Langkah 1: Ubah ke pecahan biasa
processSteps.push('<p class="font-semibold text-gray-400">1. Mengubah ke Pecahan Biasa:</p>');
if (f1.original.isMixed) {
processSteps.push(`<p class="ml-4">Pecahan 1: ${f1.original.whole} ${Math.abs(f1.original.num)}/${f1.original.den} = <span class="text-red-500">${f1.num}/${f1.den}</span></p>`);
} else {
processSteps.push(`<p class="ml-4">Pecahan 1 sudah biasa: <span class="text-red-500">${f1.num}/${f1.den}</span></p>`);
}
if (f2.original.isMixed) {
processSteps.push(`<p class="ml-4">Pecahan 2: ${f2.original.whole} ${Math.abs(f2.original.num)}/${f2.original.den} = <span class="text-red-500">${f2.num}/${f2.den}</span></p>`);
} else {
processSteps.push(`<p class="ml-4">Pecahan 2 sudah biasa: <span class="text-red-500">${f2.num}/${f2.den}</span></p>`);
}
// Langkah 2: Lakukan operasi
processSteps.push(`<p class="font-semibold text-gray-400 mt-3">2. Melakukan Operasi:</p>`);
let resultNum, resultDen;
const opSymbol = currentOperator === '*' ? '×' : currentOperator === '/' ? '÷' : currentOperator;
switch (currentOperator) {
case '+':
case '-':
resultDen = f1.den * f2.den;
const newNum1 = f1.num * f2.den;
const newNum2 = f2.num * f1.den;
processSteps.push(`<p class="ml-4">${f1.num}/${f1.den} ${opSymbol} ${f2.num}/${f2.den}</p>`);
processSteps.push(`<p class="ml-4 text-xs text-gray-500">Samakan penyebut:</p>`);
processSteps.push(`<p class="ml-4">= (${newNum1}/${resultDen}) ${opSymbol} (${newNum2}/${resultDen})</p>`);
resultNum = (currentOperator === '+') ? (newNum1 + newNum2) : (newNum1 - newNum2);
processSteps.push(`<p class="ml-4">= (${newNum1} ${opSymbol} ${newNum2}) / ${resultDen} = <span class="text-red-500">${resultNum}/${resultDen}</span></p>`);
break;
case '*':
resultNum = f1.num * f2.num;
resultDen = f1.den * f2.den;
processSteps.push(`<p class="ml-4">${f1.num}/${f1.den} ${opSymbol} ${f2.num}/${f2.den} = <span class="text-red-500">${resultNum}/${resultDen}</span></p>`);
break;
case '/':
if (f2.num === 0) throw new Error("Tidak bisa membagi dengan nol.");
resultNum = f1.num * f2.den;
resultDen = f1.den * f2.num;
processSteps.push(`<p class="ml-4">${f1.num}/${f1.den} ${opSymbol} ${f2.num}/${f2.den}</p>`);
processSteps.push(`<p class="ml-4 text-xs text-gray-500">Balik pecahan kedua dan kalikan:</p>`);
processSteps.push(`<p class="ml-4">= ${f1.num}/${f1.den} × ${f2.den}/${f2.num} = <span class="text-red-500">${resultNum}/${resultDen}</span></p>`);
break;
}
// Pindahkan tanda negatif ke pembilang jika ada di penyebut
if (resultDen < 0) {
resultNum = -resultNum;
resultDen = -resultDen;
}
// Langkah 3: Sederhanakan hasil
processSteps.push(`<p class="font-semibold text-gray-400 mt-3">3. Menyederhanakan Hasil:</p>`);
const commonDivisor = gcd(Math.abs(resultNum), Math.abs(resultDen));
const simpleNum = resultNum / commonDivisor;
const simpleDen = resultDen / commonDivisor;
if (commonDivisor > 1) {
processSteps.push(`<p class="ml-4">${resultNum}/${resultDen} ÷ ${commonDivisor}/${commonDivisor} = <span class="text-red-500">${simpleNum}/${simpleDen}</span></p>`);
} else {
processSteps.push(`<p class="ml-4">Pecahan <span class="text-red-500">${resultNum}/${resultDen}</span> sudah sederhana.</p>`);
}
// Langkah 4: Ubah ke pecahan campuran jika perlu dan siapkan teks hasil akhir
let finalResultText = '';
if (simpleDen === 1) {
finalResultText = simpleNum;
} else if (Math.abs(simpleNum) >= simpleDen) {
const wholePartResult = Math.trunc(simpleNum / simpleDen);
const numPartResult = Math.abs(simpleNum % simpleDen);
if (numPartResult === 0) {
finalResultText = wholePartResult;
} else {
finalResultText = `${wholePartResult} ${numPartResult}/${simpleDen}`;
processSteps.push(`<p class="font-semibold text-gray-400 mt-3">4. Mengubah ke Pecahan Campuran:</p>`);
processSteps.push(`<p class="ml-4">${simpleNum}/${simpleDen} = <span class="text-red-500">${finalResultText}</span></p>`);
}
} else {
finalResultText = `${simpleNum}/${simpleDen}`;
}
// Tampilkan hasil akhir
document.getElementById('resultText').innerText = finalResultText;
} catch (error) {
document.getElementById('errorText').innerText = error.message;
} finally {
document.getElementById('processText').innerHTML = processSteps.join('');
}
}
// Fungsi untuk mengubah pecahan ke desimal
function convertToDecimal() {
clearResult();
let processSteps = [];
try {
const fraction = getFractionValues('convWholeContainer', 'convNum', 'convDen');
processSteps.push('<p class="font-semibold text-gray-400">1. Mengubah ke Pecahan Biasa:</p>');
if (fraction.original.isMixed) {
processSteps.push(`<p class="ml-4">${fraction.original.whole} ${Math.abs(fraction.original.num)}/${fraction.original.den} = <span class="text-red-500">${fraction.num}/${fraction.den}</span></p>`);
} else {
processSteps.push(`<p class="ml-4">Pecahan sudah biasa: <span class="text-red-500">${fraction.num}/${fraction.den}</span></p>`);
}
processSteps.push('<p class="font-semibold text-gray-400 mt-3">2. Membagi Pembilang dengan Penyebut:</p>');
const decimalResult = fraction.num / fraction.den;
processSteps.push(`<p class="ml-4">${fraction.num} ÷ ${fraction.den} = <span class="text-red-500">${decimalResult.toLocaleString('id-ID', { maximumFractionDigits: 10 })}</span></p>`);
document.getElementById('resultText').innerText = decimalResult.toLocaleString('id-ID', { maximumFractionDigits: 10 });
} catch (error) {
document.getElementById('errorText').innerText = error.message;
} finally {
document.getElementById('processText').innerHTML = processSteps.join('');
}
}
// Inisialisasi setelah halaman dimuat
document.addEventListener('DOMContentLoaded', () => {
// Pengaturan event listener yang lebih modern dan aman, menggantikan onclick di HTML
document.getElementById('btnOperasi').addEventListener('click', () => switchMode('operasi'));
document.getElementById('btnKonversi').addEventListener('click', () => switchMode('konversi'));
// Inisialisasi tampilan awal
switchMode('operasi');
updateUI();
updateConversionUI();
});