-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
68 lines (55 loc) · 2.14 KB
/
script.js
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
const apiKey = "SUA_API_KEY"; // Substitua pela sua chave da ExchangeRate-API
const apiUrl = `https://v6.exchangerate-api.com/v6/${apiKey}/latest/USD`;
const fromCurrency = document.getElementById("fromCurrency");
const toCurrency = document.getElementById("toCurrency");
const amount = document.getElementById("amount");
const result = document.getElementById("result");
const exchangeRateText = document.getElementById("exchangeRate");
const convertBtn = document.getElementById("convertBtn");
const swapBtn = document.getElementById("swap");
let exchangeRates = {};
// Carregar moedas na lista
async function loadCurrencies() {
try {
const response = await fetch(apiUrl);
const data = await response.json();
exchangeRates = data.conversion_rates;
for (let currency in exchangeRates) {
let option1 = document.createElement("option");
let option2 = document.createElement("option");
option1.value = currency;
option1.textContent = currency;
option2.value = currency;
option2.textContent = currency;
fromCurrency.appendChild(option1);
toCurrency.appendChild(option2);
}
fromCurrency.value = "USD";
toCurrency.value = "BRL";
} catch (error) {
console.error("Erro ao carregar moedas:", error);
}
}
// Converter moeda
function convertCurrency() {
const from = fromCurrency.value;
const to = toCurrency.value;
const value = parseFloat(amount.value);
if (isNaN(value) || value <= 0) {
alert("Digite um valor válido.");
return;
}
const rate = exchangeRates[to] / exchangeRates[from];
const convertedAmount = (value * rate).toFixed(2);
result.value = convertedAmount;
exchangeRateText.textContent = `1 ${from} = ${rate.toFixed(4)} ${to}`;
}
// Inverter moedas
swapBtn.addEventListener("click", () => {
[fromCurrency.value, toCurrency.value] = [toCurrency.value, fromCurrency.value];
convertCurrency();
});
// Converter ao clicar no botão
convertBtn.addEventListener("click", convertCurrency);
// Carrega moedas na inicialização
loadCurrencies();