-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
125 lines (104 loc) · 3.68 KB
/
script.js
File metadata and controls
125 lines (104 loc) · 3.68 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
113
114
115
116
117
118
119
120
121
122
123
124
125
let input = document.querySelector(".input-box input");
let select1 = document.getElementById("from-select");
let select2 = document.getElementById("to-select");
let baseUrl =
"https://v6.exchangerate-api.com/v6/<add your api key>/latest/";
let getBtn = document.querySelector(".get-btn");
let result = document.querySelector(".result");
let mainResult = document.querySelector(".main-result");
let searchBtn = document.querySelector(".search-btn");
let fromImg = document.querySelector(".from-img");
let toImg = document.querySelector(".to-img");
let fromCurrency = document.getElementById("fromCurrency");
let toCurrency = document.getElementById("toCurrency");
let fromVal;
let toVal;
for (const element in countryList) {
let option1 = document.createElement("option");
let option2 = document.createElement("option");
select1.appendChild(option1);
select2.appendChild(option2);
option1.value = element;
option1.textContent = `${element} : ${countryList[element]}`;
option2.value = element;
option2.textContent = `${element} : ${countryList[element]}`;
}
select1.addEventListener("change", function () {
fromVal = select1.value;
});
select2.addEventListener("change", function () {
toVal = select2.value;
});
function getFromvalue() {
return fromVal;
}
function getTovalue() {
return toVal;
}
getBtn.addEventListener("click", async () => {
getExchangeRate();
});
searchBtn.addEventListener("click", async () => {
getExchangeRate();
});
async function getCountrydetails(country_code) {
// Use template literal for interpolation
let url = `https://restcountries.com/v3.1/alpha/${country_code}`;
try {
let response = await fetch(url);
if (!response.ok) {
console.error("Sorry, can't fetch country details");
return;
}
let data = await response.json();
let countryName = data[0].name.common;
let currencyCode = Object.keys(data[0].currencies)[0]; // Get the first currency code
let currencyName = data[0].currencies[currencyCode].name; // Get the currency name
console.log(currencyName);
return [countryName, currencyName];
} catch (error) {
console.error("Error fetching data:", error);
}
}
async function getExchangeRate() {
result.style.display = "none";
mainResult.style.display = "none";
result.innerText = "";
mainResult.innerText = "";
let fromVal = getFromvalue();
let fromCountry = countryList[fromVal];
let toVal = getTovalue();
let toCountry = countryList[toVal];
if (!fromVal || !toVal) {
// Check for empty, null, or undefined
alert("Please select both options.");
return;
}
fromImg.style.display = "flex";
toImg.style.display = "flex";
fromImg.src = `https://flagsapi.com/${fromCountry}/flat/64.png`;
toImg.src = `https://flagsapi.com/${toCountry}/flat/64.png`;
const [fromCountryName, fromcurrencyName] = await getCountrydetails(
fromCountry
);
const [toCountryName, toCurrencyName] = await getCountrydetails(toCountry);
fromImg.nextElementSibling.innerText = `${fromCountryName}`;
fromCurrency.innerText = `${fromcurrencyName}`;
toImg.nextElementSibling.innerText = `${toCountryName}`;
toCurrency.innerText = `${toCurrencyName}`;
let url = baseUrl.concat(fromVal);
let response = await fetch(url);
if (!response.ok) {
console.log("SORRY");
}
let data = await response.json();
console.log(data);
let conversion_rate = data.conversion_rates[toVal];
result.style.display = "flex";
result.innerText = `1 ${fromVal} = ${conversion_rate} ${toVal}`;
if (input.value.trim() != "") {
mainResult.style.display = "flex";
let final_answer = conversion_rate * input.value;
mainResult.innerHTML = `${input.value} ${fromVal} = ${final_answer} ${toVal}`;
}
}