Created
June 24, 2022 13:51
-
-
Save ashraf267/5843fc3360383d10713703836d7192b6 to your computer and use it in GitHub Desktop.
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
| // import the readline module | |
| let rl = require('readline').createInterface({ | |
| input: process.stdin, | |
| output: process.stdout, | |
| }); | |
| 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"); | |
| // ask questions | |
| rl.question("To which currency? ", (curr) => { | |
| // show currency | |
| console.log("To: > " + curr); | |
| rl.question("How much? ", (amnt) => { | |
| // show amount | |
| console.log("Amount: > " + amnt); | |
| rl.close(); | |
| convertCurrency(curr, amnt); | |
| }); | |
| }); | |
| // function to convert currency | |
| function convertCurrency(currency, amount) { | |
| let output; | |
| // run checks on the amount | |
| function checks(val) { | |
| // val; currency value | |
| if(amount < 1) { | |
| output = "The amount can not be less than 1"; | |
| } else if(isNaN(amount)) { | |
| output = "The amount has to be a number"; | |
| } else { | |
| output = amount * val; | |
| } | |
| } | |
| // find match among currencies | |
| if(currency == "JPY" || currency == "jpy") { | |
| checks(113.5); | |
| } else if(currency == "EUR" || currency == "eur") { | |
| checks(0.89); | |
| } else if(currency == "RUB" || currency == "rub") { | |
| checks(74.36); | |
| } else if(currency == "GBP" || currency == "gbp") { | |
| checks(0.75); | |
| } else if(currency == "USD" || currency == "usd") { | |
| checks(1); | |
| } else { | |
| output = "Unknown currency"; | |
| } | |
| // show result | |
| isNaN(output) ? console.log(output) : console.log("Result: " + amount + " USD equals " + parseFloat(output).toFixed(4) + " " + currency); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment