-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay41.js
More file actions
38 lines (34 loc) · 1.29 KB
/
Day41.js
File metadata and controls
38 lines (34 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//* Programming Challenge: Simple Currency Converter
//*
//? Write a function to convert an amount from one currency to another using static exchange rates.
//* Requirements:
//? Write a function named convertCurrency that takes three parameters: amount, fromCurrency, and toCurrency.
//? Use a fixed object to store exchange rates relative to a base currency (e.g., USD).
//? The function should return the converted amount in the target currency.
//? Handle conversion through USD as a base, meaning if converting from GBP to EUR, first convert GBP to USD, then USD to EUR.
const rates = {
USD: 1, // Base currency
EUR: 0.9, // 1 USD = 0.9 EUR
GBP: 0.8, // 1 USD = 0.8 GBP
INR: 82, // 1 USD = 74 INR
};
// 1 USD = 0.8
// ? = 100
const convertCurrency = (amount, fromCurrency, toCurrency) =>{
let amounInUSD = 0;
if(fromCurrency !== "USD")
{
amounInUSD = amount / rates[fromCurrency]
} else {
amounInUSD = amount;
}
let convertAmount = 0;
if(toCurrency !== "USD"){
convertAmount = amounInUSD *rates[toCurrency];
} else {
convertAmount = amountInUSD;;
}
return convertAmount;
};
// Example usage:
console.log(convertCurrency(100, "GBP", "EUR")); // Output will depend on the rates provided