-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
77 lines (71 loc) · 2.51 KB
/
index.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
69
70
71
72
73
74
75
76
77
const from = document.getElementById("from");
const to = document.getElementById("to");
const inputCurrency = document.getElementById("from-currency");
const outputCurrency = document.getElementById("to-currency");
const conversionInfo = document.getElementById("conversion-info");
let data = null;
const createOptions = (currencyObj, from, to) => {
from.innerHTML = "";
to.innerHTML = "";
for (const currency in currencyObj) {
const optionFrom = document.createElement("option");
optionFrom.value = currency;
optionFrom.text = currency;
from.appendChild(optionFrom);
const optionTo = document.createElement("option");
optionTo.value = currency;
optionTo.text = currency;
to.appendChild(optionTo);
}
};
const exchangeCurrency = (data) => {
const input = parseFloat(inputCurrency.value);
if (!isNaN(input) && input !== 0) {
outputCurrency.value = (
inputCurrency.value * data.conversion_rates[to.value]
).toFixed(2);
console.log(outputCurrency.value);
conversionInfo.innerText = `1 ${from.value} = ${
data.conversion_rates[to.value]
} ${to.value}`;
} else {
conversionInfo.innerText = `Please enter a valid amount to convert`;
outputCurrency.value = "";
}
};
const fetchAndPopulateAPI = async () => {
try {
const url = `https://v6.exchangerate-api.com/v6/4454f9cd9a5f86f42e1e0a43/latest/${from.value}`;
const response = await fetch(url);
data = await response.json();
createOptions(data.conversion_rates, from, to);
exchangeCurrency(data);
from.selectedIndex = [...from.options].findIndex(
(option) => option.value === "USD"
);
to.selectedIndex = [...to.options].findIndex(
(option) => option.value === "INR"
);
exchangeCurrency(data);
} catch (error) {
conversionInfo.innerText = `Something went wrong...`;
}
};
document.addEventListener("DOMContentLoaded", fetchAndPopulateAPI);
from.addEventListener("change", async () => {
const url = `https://v6.exchangerate-api.com/v6/4454f9cd9a5f86f42e1e0a43/latest/${from.value}`;
try {
const response = await fetch(url);
data = await response.json();
exchangeCurrency(data);
} catch (error) {
conversionInfo.innerText = `Something went wrong...`;
}
});
to.addEventListener("change", () => exchangeCurrency(data));
inputCurrency.addEventListener("input", () => exchangeCurrency(data));
const swap = document.getElementById("swap");
swap.addEventListener("click", () => {
inputCurrency.value = outputCurrency.value;
exchangeCurrency(data);
});