-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
76 lines (69 loc) · 3.45 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
69
70
71
72
73
74
75
76
const dropList = document.querySelectorAll(".drop-list select"),
fromCurrency = document.querySelector(".from select"),
toCurrency = document.querySelector(".to select"),
getButton = document.querySelector("form button");
for (let i = 0; i < dropList.length; i++) {
for (currency_code in country_code) {
// selecting USD by default as FROM currency and PKR as TO currency
let selected;
if (i == 0) {
selected = currency_code == "USD" ? "selected" : "";
} else {
if (i == 1) {
selected = currency_code == "PKR" ? "selected" : "";
}
}
// // creating option tag with passong currency code as a text and value
let optionTag = `<option value="${currency_code}" ${selected}>${currency_code}</option>`;
// // inserting option tag inside select tag
dropList[i].insertAdjacentHTML("beforeend", optionTag);
}
dropList[i].addEventListener("change", e => {
loadFlag(e.target); // calling loadflag with passing target element as an argument
});
}
function loadFlag(element) {
for (code in country_code) {
if (code == element.value) { // if currency code of country list is equal to option value
let imgTag = element.parentElement.querySelector("img"); // selecting img tag of particular drop list
// passing countru code of a selected currency code in a img url
imgTag.src = `https://flagsapi.com/${country_code[code]}/flat/64.png`
}
}
}
window.addEventListener("load", () => {
getExchangeRate();
});
getButton.addEventListener("click", e => {
e.preventDefault(); // preventing form from submitting
getExchangeRate();
});
const exchangeIcon = document.querySelector(".drop-list .icon");
exchangeIcon.addEventListener("click", () => {
let tempCode = fromCurrency.value; // temporary currency code of FROM drop list
fromCurrency.value = toCurrency.value; // passing TO currency code to FROM currency code
toCurrency.value = tempCode; // passing temporary currency code to TO currency code
loadFlag(fromCurrency); // calling loadflag with passing select element (fromCurrency) of FROM
loadFlag(toCurrency); // calling loadflag with passing select element (toCurrency) of TO
getExchangeRate()
});
function getExchangeRate() {
const amount = document.querySelector(".amount input"),
exchangeRateTxt = document.querySelector(".exchange-rate");
let amountVal = amount.value;
// if user don't enter any value or enter 0 then we'll put 1 value by defualt in the input field
if (amountVal == "" || amountVal == "0") {
amount.value = "1";
amountVal = 1;
}
exchangeRateTxt.innerText = "Getting exchange rate..."
let url = `https://v6.exchangerate-api.com/v6/521ec5eba5748c29b910c85b/latest/${fromCurrency.value}`;
// fetching api response and returning it with parsing into js obj and in another then method recieving that object
fetch(url).then(response => response.json()).then(result => {
let exchangeRate = result.conversion_rates[toCurrency.value];
let totalExchangeRate = (amountVal * exchangeRate).toFixed(2);
exchangeRateTxt.innerText = `${amountVal} ${fromCurrency.value} = ${totalExchangeRate} ${toCurrency.value}`;
}).catch(() => { // if user is offline or any other error occured while fetching data then catch function will run
exchangeRateTxt.innerText = "Something went wrong";
});
}