-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
112 lines (67 loc) · 3.38 KB
/
Copy pathscript.js
File metadata and controls
112 lines (67 loc) · 3.38 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//Importing the required modules
import { countryList } from './codes.js';
import { NEW_API_KEY } from './api_key.js';
var countries = countryList;
// This function is used for populate country name in the dropdown and set value and data to options.
function populateDropDown(countries) {
const regionNamesInEnglish = new Intl.DisplayNames(['en'], { type: 'region' });
let dropDowns = document.querySelectorAll('.fromDrop-down, .toDrop-down');
for (let dropDown of dropDowns) {
for (let countryCurrency in countries) {
let option = document.createElement('option');
option.value = countries[countryCurrency];
option.text = regionNamesInEnglish.of(countries[countryCurrency]);
option.dataset.currencyName = countryCurrency;
dropDown.appendChild(option);
if (option.text === "United States" && dropDown === document.querySelector(".fromDrop-down")) {
option.selected = "selected";
}
else if (option.text === "India" && dropDown === document.querySelector(".toDrop-down")) {
option.selected = "selected";
}
};
};
}
// This function is used for update flag on the basis of selected country.
async function updateFlag() {
document.querySelector(".fromDrop-down").addEventListener('change', (event) => {
document.querySelector(".fromFlag").src = `https://flagsapi.com/${event.target.value}/flat/64.png`;
})
document.querySelector(".toDrop-down").addEventListener('change', (event) => {
document.querySelector(".toFlag").src = `https://flagsapi.com/${event.target.value}/flat/64.png`;
})
}
// This function is used for getting the converted amount via API.
async function getConvertedAmount(amt, fromCurrency, toCurrency) {
var myHeaders = new Headers();
myHeaders.append("apikey", `${NEW_API_KEY}`);
var requestOptions = {
method: 'GET',
redirect: 'follow',
headers: myHeaders
};
let response = await fetch(`https://api.apilayer.com/fixer/convert?to=${toCurrency}&from=${fromCurrency}&amount=${amt}`, requestOptions)
response = await response.json()
let convertedAmount = Number(response["result"])
return convertedAmount;
}
// This function is used for convert the currency and updating the answer container. It is using the getConvertedAmount function to get converted amount.
async function convertCurrency() {
if (document.querySelector(".amtToBeConverted").value) {
let amt = Number(document.querySelector(".amtToBeConverted").value);
let fromCurrency = document.querySelector(".fromDrop-down").selectedOptions[0].dataset.currencyName;
let toCurrency = document.querySelector(".toDrop-down").selectedOptions[0].dataset.currencyName;
let convertedAmount = await getConvertedAmount(amt, fromCurrency, toCurrency);
let text = `${amt} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
document.querySelector(".answerText").innerHTML = text;
}
else {
alert("Please enter the amount to be converted");
}
}
// This function is used for adding an event listener to the convert button So that when every time we hit button we can run convertCurrency function.
document.querySelector(".button").addEventListener("click", convertCurrency)
//In starting of we wanted to populate dropdowns, flags according to drop-down selection and answer of currency conversion. So that we called it in starting.
populateDropDown(countries);
updateFlag()
convertCurrency()