-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
57 lines (48 loc) · 1.93 KB
/
main.ts
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import axios from 'axios';
import inquirer from 'inquirer';
async function getExchangeRates(baseCurrency: string): Promise<{ rates: Record<string, number> }> {
const apiUrl = `https://api.exchangerate-api.com/v4/latest/${baseCurrency}`;
const response = await axios.get(apiUrl);
return response.data;
}
export async function startCurrencyConverterApp() {
console.log('Welcome to the Currency Converter App!');
const { baseCurrency } = await inquirer.prompt({
type: 'input',
name: 'baseCurrency',
message: 'Enter the base currency code (e.g., USD):'
});
while (true) {
const { targetCurrency, amount } = await inquirer.prompt([
{
type: 'input',
name: 'targetCurrency',
message: 'Enter the target currency code (e.g., EUR):'
},
{
type: 'input',
name: 'amount',
message: 'Enter the amount to convert:'
}
]);
const exchangeRates = await getExchangeRates(baseCurrency);
if (exchangeRates.rates[targetCurrency]) {
const convertedAmount = parseFloat(amount) * exchangeRates.rates[targetCurrency];
console.log(`${amount} ${baseCurrency} is approximately ${convertedAmount.toFixed(2)} ${targetCurrency}\n`);
} else {
console.log(`Currency code ${targetCurrency} not found in exchange rates. Please try again.\n`);
}
const { continueConversion } = await inquirer.prompt({
type: 'confirm',
name: 'continueConversion',
message: 'Do you want to perform another conversion?',
default: true
});
if (!continueConversion) {
console.log('Thank you for using the Currency Converter App. Goodbye!');
process.exit(0);
}
}
}
// Start the Currency Converter application
startCurrencyConverterApp();