Created
July 7, 2022 22:41
-
-
Save ashraf267/e476776812c0db9acbb067863c9cfbd8 to your computer and use it in GitHub Desktop.
Another style for the currency converter (v2)
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 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, | |
} | |
]; | |
console.log("Welcome to Currency Converter!"); | |
for(let i = 0; i < currencies.length; i++) { | |
console.log("1 USD equals " + currencies[i].val + " " + currencies[i].name); | |
} | |
console.log("What do you want to convert?"); | |
let fromCurrency = "EUR"; | |
(function checkCurrency(fCurr) { | |
const findFromCurrency = currencies.find(currency => currency.name === fCurr); | |
if(!(findFromCurrency === undefined)) { | |
// true | |
// console.log("from currency is found"); | |
const toCurrency = "GBP".toUpperCase(); | |
// check if toCurrency exist | |
const findToCurrency = currencies.find(currency => currency.name === toCurrency); | |
if(!(findToCurrency === undefined)) { | |
// true | |
// console.log("the currency is: " + findToCurrency.name); | |
convertCurrency(findFromCurrency.name, findFromCurrency.val, findToCurrency.name, findToCurrency.val); | |
} else { | |
// false | |
console.log("Unknown currency"); | |
} | |
} else { | |
// false | |
console.log("Unknown currency"); | |
} | |
}(fromCurrency.toUpperCase())); | |
function convertCurrency(fCurrName, fCurrVal, tCurrName, tCurrVal) { | |
let amount = 25; | |
// check 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 { | |
// go ahead and convert | |
result = amount * tCurrVal / fCurrVal; | |
} | |
isNaN(result) ? console.log(result) : console.log("Result: " + amount + " " + fCurrName + " equals " + parseFloat(result).toFixed(4) + " " + tCurrName); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment