|
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'; |
4 | 2 | const dropdowns = document.querySelectorAll(".dropdown select"); |
5 | 3 | const btn = document.querySelector("form button"); |
6 | 4 | const fromCurr = document.querySelector(".from select"); |
7 | 5 | const toCurr = document.querySelector(".to select"); |
8 | 6 | const msg = document.querySelector(".msg"); |
9 | 7 |
|
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}`; |
26 | 40 | } |
27 | 41 |
|
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 | | -}; |
43 | 42 |
|
44 | 43 | 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 | +} |
51 | 50 |
|
52 | 51 | btn.addEventListener("click", (evt) => { |
53 | | - evt.preventDefault(); |
54 | | - updateExchangeRate(); |
| 52 | + evt.preventDefault(); |
| 53 | + updateExchangeRate(); |
55 | 54 | }); |
56 | 55 |
|
| 56 | + |
57 | 57 | window.addEventListener("load", () => { |
58 | | - updateExchangeRate(); |
59 | | -}); |
| 58 | + updateExchangeRate(); |
| 59 | +}) |
0 commit comments