Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Api Issue #32

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions CurrencyConverter/app.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const BASE_URL =
"https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies";
"https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/eur.json";

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");

// Populate dropdowns with currency options
for (let select of dropdowns) {
for (currCode in countryList) {
let newOption = document.createElement("option");
Expand All @@ -25,22 +26,41 @@ for (let select of dropdowns) {
});
}

// Fetch exchange rates and update message
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}`;
try {
let response = await fetch(BASE_URL);
let data = await response.json();

// Convert currency values to lowercase for consistency with the API data
let fromCurrencyCode = fromCurr.value.toLowerCase();
let toCurrencyCode = toCurr.value.toLowerCase();

// Get the rates for the selected currencies
let fromRate = data.eur[fromCurrencyCode]; // 'fromCurr' value relative to EUR
let toRate = data.eur[toCurrencyCode]; // 'toCurr' value relative to EUR

// Calculate conversion rate: EUR -> fromCurr and EUR -> toCurr
let rate = toRate / fromRate;
let finalAmount = amtVal * rate;

msg.innerText = `${amtVal} ${fromCurr.value} = ${finalAmount.toFixed(2)} ${
toCurr.value
}`;
} catch (error) {
console.error("Error fetching exchange rates:", error);
msg.innerText = "Error: Unable to retrieve exchange rates.";
}
};

// Update flag image when currency changes
const updateFlag = (element) => {
let currCode = element.value;
let countryCode = countryList[currCode];
Expand Down