Update script.js
Browse files
script.js
CHANGED
@@ -1,53 +1,42 @@
|
|
1 |
-
// Menangani submit form untuk konversi mata uang
|
2 |
document.getElementById('currencyForm').addEventListener('submit', async function(event) {
|
3 |
event.preventDefault();
|
4 |
|
5 |
-
// Ambil nilai input dari user
|
6 |
const amount = document.getElementById('amount').value;
|
7 |
const fromCurrency = document.getElementById('fromCurrency').value;
|
8 |
const toCurrency = document.getElementById('toCurrency').value;
|
9 |
const resultDiv = document.getElementById('result');
|
10 |
|
11 |
-
// Validasi jumlah yang dimasukkan
|
12 |
if (!amount || amount <= 0) {
|
13 |
resultDiv.innerHTML = "Please enter a valid amount!";
|
14 |
return;
|
15 |
}
|
16 |
|
17 |
try {
|
18 |
-
const apiKey = '3ebe2ccf9eeea2aaef280201';
|
19 |
const url = `https://v6.exchangerate-api.com/v6/${apiKey}/latest/${fromCurrency}`;
|
20 |
-
|
21 |
-
// Mengambil data dari API
|
22 |
const response = await fetch(url);
|
23 |
const data = await response.json();
|
24 |
|
25 |
-
// Menangani respon API yang error
|
26 |
if (data.result === 'error') {
|
27 |
resultDiv.innerHTML = `Error: ${data['error-type']}`;
|
28 |
} else {
|
29 |
-
// Menyusun nilai konversi dan menampilkannya
|
30 |
const rate = data.conversion_rates[toCurrency];
|
31 |
const convertedAmount = (amount * rate).toFixed(2);
|
32 |
resultDiv.innerHTML = `${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
|
33 |
}
|
34 |
} catch (error) {
|
35 |
resultDiv.innerHTML = "Error fetching conversion rate.";
|
36 |
-
console.error("Error:", error);
|
37 |
}
|
38 |
});
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
const
|
43 |
-
const toCurrency = document.getElementById("toCurrency");
|
44 |
|
45 |
-
// Tukar nilai antara dropdown
|
46 |
const temp = fromCurrency.value;
|
47 |
fromCurrency.value = toCurrency.value;
|
48 |
toCurrency.value = temp;
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
resultDiv.innerHTML = ""; // Kosongkan hasil jika ada perubahan
|
53 |
-
}
|
|
|
|
|
1 |
document.getElementById('currencyForm').addEventListener('submit', async function(event) {
|
2 |
event.preventDefault();
|
3 |
|
|
|
4 |
const amount = document.getElementById('amount').value;
|
5 |
const fromCurrency = document.getElementById('fromCurrency').value;
|
6 |
const toCurrency = document.getElementById('toCurrency').value;
|
7 |
const resultDiv = document.getElementById('result');
|
8 |
|
|
|
9 |
if (!amount || amount <= 0) {
|
10 |
resultDiv.innerHTML = "Please enter a valid amount!";
|
11 |
return;
|
12 |
}
|
13 |
|
14 |
try {
|
15 |
+
const apiKey = '3ebe2ccf9eeea2aaef280201';
|
16 |
const url = `https://v6.exchangerate-api.com/v6/${apiKey}/latest/${fromCurrency}`;
|
|
|
|
|
17 |
const response = await fetch(url);
|
18 |
const data = await response.json();
|
19 |
|
|
|
20 |
if (data.result === 'error') {
|
21 |
resultDiv.innerHTML = `Error: ${data['error-type']}`;
|
22 |
} else {
|
|
|
23 |
const rate = data.conversion_rates[toCurrency];
|
24 |
const convertedAmount = (amount * rate).toFixed(2);
|
25 |
resultDiv.innerHTML = `${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
|
26 |
}
|
27 |
} catch (error) {
|
28 |
resultDiv.innerHTML = "Error fetching conversion rate.";
|
29 |
+
console.error("Error:", error);
|
30 |
}
|
31 |
});
|
32 |
|
33 |
+
document.getElementById('swapButton').addEventListener('click', function() {
|
34 |
+
const fromCurrency = document.getElementById('fromCurrency');
|
35 |
+
const toCurrency = document.getElementById('toCurrency');
|
|
|
36 |
|
|
|
37 |
const temp = fromCurrency.value;
|
38 |
fromCurrency.value = toCurrency.value;
|
39 |
toCurrency.value = temp;
|
40 |
|
41 |
+
document.getElementById('result').innerHTML = "";
|
42 |
+
});
|
|
|
|