Spaces:
Running
Running
Update script.js
Browse files
script.js
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
// Fungsi untuk melakukan konversi mata uang
|
2 |
async function convertCurrency() {
|
3 |
const amount = document.getElementById('amount').value;
|
4 |
const fromCurrency = document.getElementById('from-currency').value;
|
@@ -8,15 +7,12 @@ async function convertCurrency() {
|
|
8 |
|
9 |
if (amount && fromCurrency && toCurrency) {
|
10 |
try {
|
11 |
-
const response = await fetch(`https://v6.exchangerate-api.com/v6/
|
12 |
const data = await response.json();
|
13 |
const rate = data.rates[toCurrency];
|
14 |
const convertedAmount = (amount * rate).toFixed(2);
|
15 |
|
16 |
-
// Menampilkan hasil konversi
|
17 |
result.innerHTML = `${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
|
18 |
-
|
19 |
-
// Sembunyikan tombol Convert setelah konversi otomatis
|
20 |
convertBtn.style.display = 'none';
|
21 |
} catch (error) {
|
22 |
result.innerText = "Error fetching exchange rates!";
|
@@ -24,24 +20,18 @@ async function convertCurrency() {
|
|
24 |
}
|
25 |
}
|
26 |
|
27 |
-
// Fungsi untuk menukar mata uang From dan To
|
28 |
function swapCurrencies() {
|
29 |
const fromCurrency = document.getElementById('from-currency');
|
30 |
const toCurrency = document.getElementById('to-currency');
|
31 |
|
32 |
-
// Tukar nilai mata uang
|
33 |
const temp = fromCurrency.value;
|
34 |
fromCurrency.value = toCurrency.value;
|
35 |
toCurrency.value = temp;
|
36 |
|
37 |
-
// Lakukan konversi otomatis setelah swap
|
38 |
convertCurrency();
|
39 |
}
|
40 |
|
41 |
-
// Event listener untuk input amount agar otomatis konversi
|
42 |
document.getElementById('amount').addEventListener('input', convertCurrency);
|
43 |
-
|
44 |
-
// Event listener untuk perubahan mata uang (From dan To) agar tombol Convert muncul kembali
|
45 |
document.getElementById('from-currency').addEventListener('change', () => {
|
46 |
document.getElementById('convert-btn').style.display = 'block';
|
47 |
document.getElementById('result').innerHTML = '';
|
@@ -50,27 +40,4 @@ document.getElementById('from-currency').addEventListener('change', () => {
|
|
50 |
document.getElementById('to-currency').addEventListener('change', () => {
|
51 |
document.getElementById('convert-btn').style.display = 'block';
|
52 |
document.getElementById('result').innerHTML = '';
|
53 |
-
});
|
54 |
-
|
55 |
-
// Mengubah warna latar belakang ketika menggulir
|
56 |
-
window.onscroll = function() {
|
57 |
-
const navbar = document.querySelector('.navbar');
|
58 |
-
const headline = document.querySelector('.headline');
|
59 |
-
const steps = document.querySelector('.steps');
|
60 |
-
const footer = document.querySelector('.footer');
|
61 |
-
|
62 |
-
if (window.scrollY > 200) {
|
63 |
-
navbar.style.backgroundColor = '#004d00'; // Darker Green
|
64 |
-
headline.style.backgroundColor = '#004d00'; // Darker Green
|
65 |
-
steps.style.backgroundColor = '#004d00'; // Darker Green
|
66 |
-
footer.style.backgroundColor = '#666666'; // Darker Gray
|
67 |
-
} else {
|
68 |
-
navbar.style.backgroundColor = '#006400'; // Original Green
|
69 |
-
headline.style.backgroundColor = '#006400'; // Original Green
|
70 |
-
steps.style.backgroundColor = '#006400'; // Original Green
|
71 |
-
footer.style.backgroundColor = '#808080'; // Original Gray
|
72 |
-
}
|
73 |
-
};
|
74 |
-
|
75 |
-
// Fungsi untuk menampilkan hasil swap mata uang secara otomatis setelah pemilihan
|
76 |
-
document.getElementById('swap-btn').addEventListener('click', swapCurrencies);
|
|
|
|
|
1 |
async function convertCurrency() {
|
2 |
const amount = document.getElementById('amount').value;
|
3 |
const fromCurrency = document.getElementById('from-currency').value;
|
|
|
7 |
|
8 |
if (amount && fromCurrency && toCurrency) {
|
9 |
try {
|
10 |
+
const response = await fetch(`https://v6.exchangerate-api.com/v6/3ebe2ccf9eeea2aaef280201/latest/${fromCurrency}`);
|
11 |
const data = await response.json();
|
12 |
const rate = data.rates[toCurrency];
|
13 |
const convertedAmount = (amount * rate).toFixed(2);
|
14 |
|
|
|
15 |
result.innerHTML = `${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
|
|
|
|
|
16 |
convertBtn.style.display = 'none';
|
17 |
} catch (error) {
|
18 |
result.innerText = "Error fetching exchange rates!";
|
|
|
20 |
}
|
21 |
}
|
22 |
|
|
|
23 |
function swapCurrencies() {
|
24 |
const fromCurrency = document.getElementById('from-currency');
|
25 |
const toCurrency = document.getElementById('to-currency');
|
26 |
|
|
|
27 |
const temp = fromCurrency.value;
|
28 |
fromCurrency.value = toCurrency.value;
|
29 |
toCurrency.value = temp;
|
30 |
|
|
|
31 |
convertCurrency();
|
32 |
}
|
33 |
|
|
|
34 |
document.getElementById('amount').addEventListener('input', convertCurrency);
|
|
|
|
|
35 |
document.getElementById('from-currency').addEventListener('change', () => {
|
36 |
document.getElementById('convert-btn').style.display = 'block';
|
37 |
document.getElementById('result').innerHTML = '';
|
|
|
40 |
document.getElementById('to-currency').addEventListener('change', () => {
|
41 |
document.getElementById('convert-btn').style.display = 'block';
|
42 |
document.getElementById('result').innerHTML = '';
|
43 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|