diff --git a/CurrencyConverter/app.js b/CurrencyConverter/app.js index 6009ab0..79f8a27 100644 --- a/CurrencyConverter/app.js +++ b/CurrencyConverter/app.js @@ -1,59 +1,62 @@ -const BASE_URL = - "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies"; +const today = new Date(); +const yyyy = today.getFullYear(); +const mm = String(today.getMonth() + 1).padStart(2, '0'); +const dd = String(today.getDate()).padStart(2, '0'); +const latestDate = `${yyyy}-${mm}-${dd}`; +const BASE_URL = `https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@${latestDate}/v1`; +const dropdown = document.querySelectorAll(".dropdown select") +const button = document.querySelector("form button"); +const fromCurr = document.querySelector(".from select") +const tocurr = document.querySelector(".to select") +let msg = document.querySelector(".msg"); -const dropdowns = document.querySelectorAll(".dropdown select"); -const btn = document.querySelector("form button"); -const fromCurr = document.querySelector(".from select"); -const toCurr = document.querySelector(".to select"); -const msg = document.querySelector(".msg"); - -for (let select of dropdowns) { - for (currCode in countryList) { - let newOption = document.createElement("option"); - newOption.innerText = currCode; - newOption.value = currCode; - if (select.name === "from" && currCode === "USD") { - newOption.selected = "selected"; - } else if (select.name === "to" && currCode === "INR") { - newOption.selected = "selected"; +const updateExchangeRate = async () => { + let amount = document.querySelector("form input"); + if (amount.value === "" || amount.value < 1) { + amount.value = 1; } - select.append(newOption); - } - - select.addEventListener("change", (evt) => { - updateFlag(evt.target); - }); + const from = fromCurr.value.toLowerCase(); + const to = tocurr.value.toLowerCase(); + const URL = `${BASE_URL}/currencies/${from}.json`; + const response = await fetch(URL); + const data = await response.json(); + const rate = data[from][to]; + let convertedAmount = (amount.value * rate).toFixed(3); + msg.innerText = `${amount.value}${fromCurr.value} = ${convertedAmount}${tocurr.value}`; } -const updateExchangeRate = async () => { - let amount = document.querySelector(".amount input"); - let amtVal = amount.value; - if (amtVal === "" || amtVal < 1) { - amtVal = 1; - amount.value = "1"; - } - const URL = `${BASE_URL}/${fromCurr.value.toLowerCase()}/${toCurr.value.toLowerCase()}.json`; - let response = await fetch(URL); - let data = await response.json(); - let rate = data[toCurr.value.toLowerCase()]; - - let finalAmount = amtVal * rate; - msg.innerText = `${amtVal} ${fromCurr.value} = ${finalAmount} ${toCurr.value}`; -}; +for (let select of dropdown) { + for (curcode in countryList) { + let newoption = document.createElement("option") + newoption.innerText = curcode; + newoption.value = curcode; + if (select.name == "from" && curcode === "USD") { + newoption.selected = "selected" + } + if (select.name == "to" && curcode === "INR") { + newoption.selected = "selected" + } + select.append(newoption) + } + select.addEventListener("change", (evt) => { + updateflag(evt.target); + }); +} -const updateFlag = (element) => { - let currCode = element.value; - let countryCode = countryList[currCode]; - let newSrc = `https://flagsapi.com/${countryCode}/flat/64.png`; - let img = element.parentElement.querySelector("img"); - img.src = newSrc; -}; +const updateflag = (element) => { + let curcode = element.value; + let countrycode = countryList[curcode]; + let newsouce = `https://flagsapi.com/${countrycode}/flat/64.png`; + let img = element.parentElement.querySelector("img"); + img.src = newsouce; +} -btn.addEventListener("click", (evt) => { - evt.preventDefault(); - updateExchangeRate(); +button.addEventListener("click", (evt) => { + evt.preventDefault(); + updateExchangeRate(); }); -window.addEventListener("load", () => { - updateExchangeRate(); -}); +window.addEventListener("load", async () => { + updateExchangeRate(); +}) +