Download script.js from GarGerry/Testinghh: direct link, hf CLI and curl.
- Browser
- Download file 2.79 kB
-
https://huggingface.co/spaces/GarGerry/Testinghh/resolve/7ae1eb2320f1074175ee50afe59cd2cb94fbb4c7/script.js
- Command line
-
hf download hf://spaces/GarGerry/Testinghh@7ae1eb2320f1074175ee50afe59cd2cb94fbb4c7/script.js
-
curl -L -o script.js https://huggingface.co/spaces/GarGerry/Testinghh/resolve/7ae1eb2320f1074175ee50afe59cd2cb94fbb4c7/script.js
2.79 kB
| // Ambil elemen | |
| const amountInput = document.getElementById('amount'); | |
| const fromCurrency = document.getElementById('fromCurrency'); | |
| const toCurrency = document.getElementById('toCurrency'); | |
| const resultDiv = document.getElementById('result'); | |
| const convertButton = document.createElement('button'); | |
| convertButton.textContent = 'Convert'; | |
| convertButton.type = 'button'; | |
| convertButton.id = 'convertButton'; | |
| document.getElementById('currencyForm').appendChild(convertButton); | |
| // Tombol tampil secara default | |
| let isInitialConversion = true; | |
| async function convertCurrency() { | |
| const amount = amountInput.value; | |
| // Validasi input jumlah | |
| if (!amount || amount <= 0) { | |
| resultDiv.innerHTML = "Please enter a valid amount!"; | |
| return; | |
| } | |
| try { | |
| const apiKey = '3ebe2ccf9eeea2aaef280201'; // API Key | |
| const url = `https://v6.exchangerate-api.com/v6/${apiKey}/latest/${fromCurrency.value}`; | |
| // Ambil data dari API | |
| const response = await fetch(url); | |
| const data = await response.json(); | |
| // Tangani error API | |
| if (data.result === 'error') { | |
| resultDiv.innerHTML = `Error: ${data['error-type']}`; | |
| } else { | |
| const rate = data.conversion_rates[toCurrency.value]; | |
| const convertedAmount = (amount * rate).toFixed(2); | |
| resultDiv.innerHTML = `${amount} ${fromCurrency.value} = ${convertedAmount} ${toCurrency.value}`; | |
| // Sembunyikan tombol setelah konversi pertama | |
| if (isInitialConversion) { | |
| convertButton.style.display = 'none'; | |
| isInitialConversion = false; | |
| } | |
| } | |
| } catch (error) { | |
| resultDiv.innerHTML = "Error fetching conversion rate."; | |
| console.error("Error:", error); | |
| } | |
| } | |
| // Fungsi Swap | |
| document.getElementById('swapButton').addEventListener('click', function () { | |
| const tempCurrency = fromCurrency.value; | |
| fromCurrency.value = toCurrency.value; | |
| toCurrency.value = tempCurrency; | |
| if (amountInput.value && amountInput.value > 0) { | |
| convertCurrency(); // Konversi ulang setelah swap | |
| } | |
| }); | |
| // Event untuk tombol Convert (klik tombol manual) | |
| convertButton.addEventListener('click', function () { | |
| convertCurrency(); | |
| }); | |
| // Event otomatis konversi setelah jumlah diisi | |
| amountInput.addEventListener('input', function () { | |
| if (!isInitialConversion) { | |
| convertCurrency(); // Konversi otomatis | |
| } | |
| }); | |
| // Event tampilkan tombol jika mata uang diubah | |
| fromCurrency.addEventListener('change', function () { | |
| convertButton.style.display = 'block'; // Tampilkan tombol | |
| isInitialConversion = true; // Reset status awal | |
| resultDiv.innerHTML = ""; // Reset hasil | |
| }); | |
| toCurrency.addEventListener('change', function () { | |
| convertButton.style.display = 'block'; // Tampilkan tombol | |
| isInitialConversion = true; // Reset status awal | |
| resultDiv.innerHTML = ""; // Reset hasil | |
| }); | |