consolecurrencyconverter
Version:
Console app currency converter
57 lines (52 loc) • 1.29 kB
text/typescript
import inquirer from 'inquirer';
// create currency converter command line
const rates: any = {
USD: 1,
EUR: 0.85,
GBP: 0.75,
JPY: 110.32,
NGN: 411.33
};
const questions = [
{
type: 'input',
name: 'amount',
message: 'Enter amount to convert'
},
{
type: 'list',
name: 'from',
message: 'From currency',
choices: ['USD', 'EUR', 'GBP', 'JPY', 'NGN']
},
{
type: 'list',
name: 'to',
message: 'To currency',
choices: ['USD', 'EUR', 'GBP', 'JPY', 'NGN']
},
{
type: 'list',
name: 'command',
message: 'What do you want to do?',
choices: ['Convert', 'Exit']
}
]
function convert(amount: number, from: string, to: string) {
const convertedAmount = (amount * rates[from]) / rates[to];
return convertedAmount;
}
function init() {
inquirer.prompt(questions)
.then(answers => {
if (answers.command === 'Convert') {
console.log(convert(answers.amount, answers.from, answers.to));
init();
}
})
.catch(error => {
console.log(error);
});
}
init();