Created
July 4, 2022 12:56
-
-
Save ashraf267/a22cfa7226243991a5113ae192af087b to your computer and use it in GitHub Desktop.
Second version of the Currency Converter that worked.
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 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 checkCurrency(fCurr) { | |
let isFound = false; | |
let fromCurrVal; | |
let currArray; | |
currencies.forEach(function(itm, ind, arr) { | |
if(itm.name == fCurr) { | |
// true | |
isFound = true; | |
fromCurrVal = itm.val; | |
currArray = arr; | |
} | |
}); | |
(!(isFound == true)) ? console.log("Unknown currency") : convertCurrency(fromCurrVal, currArray); | |
}(fromCurrency.toUpperCase())); | |
function convertCurrency(fCurrVal, currArr) { | |
let isFound = false; | |
let toCurrency = input("To:"); | |
let toCurrVal; | |
currArr.forEach(function(item) { | |
if(item.name == toCurrency.toUpperCase()) { | |
// true | |
isFound = true; | |
toCurrVal = item.val; | |
} | |
}); | |
let amount; | |
let result; | |
if(!(isFound == true)) { | |
result = "Unknown currency"; | |
} else { | |
amount = input("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 { | |
// all tests passed. Go ahead. | |
result = amount * toCurrVal / 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