Skip to content

Instantly share code, notes, and snippets.

@ashraf267
Last active June 25, 2022 13:58
Show Gist options
  • Save ashraf267/be0ca7b00f47e06ecf31a895341ffb88 to your computer and use it in GitHub Desktop.
Save ashraf267/be0ca7b00f47e06ecf31a895341ffb88 to your computer and use it in GitHub Desktop.
This is the only solution that passed all test. Alhamdulillah! I am proud of this one.
let input = require('sync-input');
console.log("Welcome to Currency Converter!");
console.log(`1 USD equals 1 USD
1 USD equals 113.5 JPY
1 USD equals 0.89 EUR
1 USD equals 74.36 RUB
1 USD equals 0.75 GBP`);
console.log("I can convert USD to these currencies: JPY, EUR, RUB, USD, GBP");
console.log("Type the currency you wish to convert: USD");
let currency = input("To:");
(function checkCurrency() {
if(currency == "JPY" || currency == "jpy" || currency == "JpY") {
// do something
convertCurrency(currency, 113.5);
} else if(currency == "EUR" || currency == "eur" || currency == "EuR") {
// do something
convertCurrency(currency, 0.89);
} else if(currency == "RUB" || currency == "rub" || currency == "RuB") {
// do something
convertCurrency(currency, 74.36);
} else if(currency == "GBP" || currency == "gbp" || currency == "GbP") {
// do something
convertCurrency(currency, 0.75);
} else if(currency == "USD" || currency == "usd" || currency == "UsD") {
// do something
convertCurrency(currency, 1);
} else {
// unknown currency
console.log("Unknown currency");
}
}());
function convertCurrency(curr, val) {
let amount = input("Amount:");
let result;
// checks on amount
if(amount < 1) {
result = "The amount can not be less than 1";
} else if(isNaN(amount)) {
result = "The amount has to be a number";
} else {
result = amount * val;
}
// show result
isNaN(result) ? console.log(result) : console.log("Result: " + amount + " USD equals " + parseFloat(result).toFixed(4) + " " + curr.toUpperCase());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment