diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6f3a291 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5501 +} \ No newline at end of file diff --git a/CurrencyConverter/app.js b/CurrencyConverter/app.js index 6009ab0..3c527d7 100644 --- a/CurrencyConverter/app.js +++ b/CurrencyConverter/app.js @@ -1,11 +1,11 @@ -const BASE_URL = - "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies"; +const BASE_URL ="https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies"; const dropdowns = document.querySelectorAll(".dropdown select"); -const btn = document.querySelector("form button"); +const btn = document.querySelector("#ExchangeButton"); const fromCurr = document.querySelector(".from select"); const toCurr = document.querySelector(".to select"); const msg = document.querySelector(".msg"); +const swap = document.querySelector("#swap"); for (let select of dropdowns) { for (currCode in countryList) { @@ -25,20 +25,48 @@ for (let select of dropdowns) { }); } +const swapping = () => { + // Swap the values of fromCurr and toCurr select elements + + const temp = fromCurr.value; + fromCurr.value = toCurr.value; + toCurr.value = temp; + + // Update the flag images based on the new selections + updateFlag(fromCurr); + updateFlag(toCurr); + + // Update the exchange rate based on the new selections + updateExchangeRate(); +}; + +swap.addEventListener("click", (evt) => { + evt.preventDefault(); // Prevent the default button behavior + swapping(); // Execute the swapping function +}); + const updateExchangeRate = async () => { + let amount = document.querySelector(".amount input"); let amtVal = amount.value; + // console.log(amount.value) if (amtVal === "" || amtVal < 1) { amtVal = 1; amount.value = "1"; } - const URL = `${BASE_URL}/${fromCurr.value.toLowerCase()}/${toCurr.value.toLowerCase()}.json`; + // console.log(amount.value); + const URL = `${BASE_URL}/${fromCurr.value.toLowerCase()}.json`; let response = await fetch(URL); let data = await response.json(); - let rate = data[toCurr.value.toLowerCase()]; - + // console.log(data) + + let rate = data[fromCurr.value.toLowerCase()][toCurr.value.toLowerCase()]; + + // console.log(data); let finalAmount = amtVal * rate; + // console.log(finalAmount); msg.innerText = `${amtVal} ${fromCurr.value} = ${finalAmount} ${toCurr.value}`; + // console.log(msg); }; const updateFlag = (element) => { @@ -49,11 +77,14 @@ const updateFlag = (element) => { img.src = newSrc; }; -btn.addEventListener("click", (evt) => { - evt.preventDefault(); - updateExchangeRate(); +btn.addEventListener("click", async (evt) => { + evt.preventDefault(); // Prevent the default form submission behavior + + // Update the exchange rate + await updateExchangeRate(); }); + window.addEventListener("load", () => { updateExchangeRate(); -}); +}); \ No newline at end of file diff --git a/CurrencyConverter/index.html b/CurrencyConverter/index.html index 40ceb46..23f6289 100644 --- a/CurrencyConverter/index.html +++ b/CurrencyConverter/index.html @@ -3,8 +3,8 @@
-