From b357813db4950cd41d9c69cd2e5f38a935f92852 Mon Sep 17 00:00:00 2001 From: Jagadeesh Bobbili Date: Sun, 4 Jan 2026 13:17:03 +0530 Subject: [PATCH] Refactor currency exchange API integration Updated the currency exchange API integration to use the latest version and modified the data access structure accordingly. --- CurrencyConverter/app.js | 74 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 6 deletions(-) diff --git a/CurrencyConverter/app.js b/CurrencyConverter/app.js index 6009ab0..173f5ee 100644 --- a/CurrencyConverter/app.js +++ b/CurrencyConverter/app.js @@ -25,6 +25,32 @@ for (let select of dropdowns) { }); } +const BASE_URL = + "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies"; + +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"; + } + select.append(newOption); + + + select.addEventListener("change", (evt) => { + updateFlag(evt.target); + }); +} const updateExchangeRate = async () => { let amount = document.querySelector(".amount input"); let amtVal = amount.value; @@ -32,15 +58,51 @@ const updateExchangeRate = async () => { 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}`; + // 1. Updated URL: The new API uses /v1/currencies/{from}.json + const fromCode = fromCurr.value.toLowerCase(); + const toCode = toCurr.value.toLowerCase(); + const URL = `https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/${fromCode}.json`; + + try { + let response = await fetch(URL); + + if (!response.ok) { + msg.innerText = "Error fetching exchange rate."; + return; + } + + let data = await response.json(); + + // 2. Updated Data Access: The new API returns { "usd": { "inr": 83.5 } } + let rate = data[fromCode][toCode]; + + let finalAmount = (amtVal * rate).toFixed(2); + msg.innerText = `${amtVal} ${fromCurr.value} = ${finalAmount} ${toCurr.value}`; + } catch (error) { + msg.innerText = "Failed to load data. Check your internet."; + console.error(error); + } +}; + +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; }; +btn.addEventListener("click", (evt) => { + evt.preventDefault(); + updateExchangeRate(); +}); + +window.addEventListener("load", () => { + updateExchangeRate(); +}); +} + const updateFlag = (element) => { let currCode = element.value; let countryCode = countryList[currCode];