Created
July 2, 2022 10:37
-
-
Save ashraf267/c73d6955ad70b9a648a6cf3bc8dce7bf to your computer and use it in GitHub Desktop.
work in progress solution.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("What do you want to convert?"); | |
let fromCurrency = input("From:"); | |
let toCurrency = input("To:"); | |
let currencies = [ | |
{ | |
name: "JPY", | |
val: 113.5, | |
}, | |
{ | |
name: "EUR", | |
val: 0.89, | |
}, | |
{ | |
name: "RUB", | |
val: 74.36, | |
}, | |
{ | |
name: "GBP", | |
val: 0.75, | |
}, | |
{ | |
name: "USD", | |
val: 1, | |
}, | |
]; | |
(function checkCurrencies(fCurr, tCurr) { | |
let isFound = false; | |
let fromCurrVal; | |
let toCurrVal; | |
currencies.forEach(function(itm, ind, arr) { | |
if(itm.name == fCurr) { | |
// true | |
arr.forEach(function(item) { | |
if(item.name == tCurr) { | |
// true | |
isFound = true; | |
fromCurrVal = itm.val; | |
toCurrVal = item.val; | |
} | |
}); | |
} | |
}); | |
(!(isFound == true)) ? console.log("Unknown currency") : convertCurrency(fromCurrVal, toCurrVal); | |
}(fromCurrency, toCurrency)); | |
function convertCurrency(fCurrVal, tCurrVal) { | |
let amount = input("Amount:"); | |
let result = ""; | |
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 { | |
// all good! convert the currency | |
result = amount * tCurrVal / fCurrVal; | |
} | |
isNaN(result) ? console.log(result) : console.log("Result: " + amount + " " + fromCurrency.toUpperCase() + " equals " + parseFloat(result).toFixed(4) + " " + toCurrency.toUpperCase()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment