Skip to content

Commit 3c11872

Browse files
authored
Update app.js
The API URI is changed, so the old method to fetch rate from the api is not working, this is a solution for the problem, all country rates converting with the updated base URI and the rate value set to let rate = fromData[fromCurr.value.toLowerCase()][toCurr.value.toLowerCase()]
1 parent 61a8830 commit 3c11872

File tree

1 file changed

+44
-44
lines changed

1 file changed

+44
-44
lines changed

CurrencyConverter/app.js

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
1-
const BASE_URL =
2-
"https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies";
3-
1+
const BASE_URL = 'https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@2025.3.14/v1/currencies';
42
const dropdowns = document.querySelectorAll(".dropdown select");
53
const btn = document.querySelector("form button");
64
const fromCurr = document.querySelector(".from select");
75
const toCurr = document.querySelector(".to select");
86
const msg = document.querySelector(".msg");
97

10-
for (let select of dropdowns) {
11-
for (currCode in countryList) {
12-
let newOption = document.createElement("option");
13-
newOption.innerText = currCode;
14-
newOption.value = currCode;
15-
if (select.name === "from" && currCode === "USD") {
16-
newOption.selected = "selected";
17-
} else if (select.name === "to" && currCode === "INR") {
18-
newOption.selected = "selected";
19-
}
20-
select.append(newOption);
21-
}
22-
23-
select.addEventListener("change", (evt) => {
24-
updateFlag(evt.target);
25-
});
8+
for(let select of dropdowns) {
9+
for (currCode in countryList) {
10+
let newOption = document.createElement("option");
11+
newOption.innerText = currCode;
12+
newOption.value = currCode;
13+
select.append(newOption);
14+
if(select.name === "from" && currCode === "USD") {
15+
newOption.selected = "selected";
16+
} else if(select.name === "to" && currCode === "INR") {
17+
newOption.selected = "selected";
18+
}
19+
}
20+
21+
select.addEventListener("change", (evt) => {
22+
updateFlag(evt.target);
23+
});
24+
}
25+
26+
27+
const updateExchangeRate = async () => {
28+
let amount = document.querySelector(".amount input");
29+
let amtVal = amount.value;
30+
if(amtVal === "" || amtVal < 1) {
31+
amtVal = 1;
32+
amount.value = "1";
33+
}
34+
const fromURL = `${BASE_URL}/${fromCurr.value.toLowerCase()}.json`;
35+
let fromResponse = await fetch(fromURL);
36+
let fromData = await fromResponse.json();
37+
let rate = fromData[fromCurr.value.toLowerCase()][toCurr.value.toLowerCase()];
38+
let finalAmount = amtVal * rate;
39+
msg.innerText = `${amtVal} ${fromCurr.value} = ${finalAmount.toFixed(2)} ${toCurr.value}`;
2640
}
2741

28-
const updateExchangeRate = async () => {
29-
let amount = document.querySelector(".amount input");
30-
let amtVal = amount.value;
31-
if (amtVal === "" || amtVal < 1) {
32-
amtVal = 1;
33-
amount.value = "1";
34-
}
35-
const URL = `${BASE_URL}/${fromCurr.value.toLowerCase()}/${toCurr.value.toLowerCase()}.json`;
36-
let response = await fetch(URL);
37-
let data = await response.json();
38-
let rate = data[toCurr.value.toLowerCase()];
39-
40-
let finalAmount = amtVal * rate;
41-
msg.innerText = `${amtVal} ${fromCurr.value} = ${finalAmount} ${toCurr.value}`;
42-
};
4342

4443
const updateFlag = (element) => {
45-
let currCode = element.value;
46-
let countryCode = countryList[currCode];
47-
let newSrc = `https://flagsapi.com/${countryCode}/flat/64.png`;
48-
let img = element.parentElement.querySelector("img");
49-
img.src = newSrc;
50-
};
44+
let currCode = element.value;
45+
let countryCode = countryList[currCode];
46+
let newSrc = `https://flagsapi.com/${countryCode}/shiny/64.png`;
47+
let img = element.parentElement.querySelector("img");
48+
img.src = newSrc;
49+
}
5150

5251
btn.addEventListener("click", (evt) => {
53-
evt.preventDefault();
54-
updateExchangeRate();
52+
evt.preventDefault();
53+
updateExchangeRate();
5554
});
5655

56+
5757
window.addEventListener("load", () => {
58-
updateExchangeRate();
59-
});
58+
updateExchangeRate();
59+
})

0 commit comments

Comments
 (0)