DEV Community

Cover image for Currency Converter App with Node.js
Bek Brace
Bek Brace

Posted on • Updated on

Currency Converter App with Node.js

Hey there! Are you interested in building a currency converter using JavaScript? Well, you're in luck! In this tutorial, we'll walk you through the process of creating a powerful currency converter application that allows you to convert between various currencies with ease. Let's dive in!

To begin, we'll need a few dependencies to get started. Make sure you have axios, inquirer, and chalk installed in your project. These libraries will help us make API requests, interact with the user through the command line, and add some colorful styling to our output.
GitHub Repo: https://github.com/BekBrace/currency-converter-cli

Next, let's start by declaring our variables. We have an array called "currencies" that holds the currency codes we want to support. Feel free to modify this list to suit your needs. We also have an array of "questions" using the inquirer library, which will prompt the user for the conversion details: the currency to convert from, the currency to convert to, and the amount.

Now comes the exciting part. We'll define an asynchronous function called "convertCurrency". This function takes in the "from" currency, the "to" currency, and the amount to be converted. Inside this function, we'll use the axios library to make a GET request to the Frankfurter API, passing in the relevant parameters. The response will contain the conversion rate, and we'll extract the result and return it, ensuring that it's rounded to two decimal places.

Moving on, we'll create another asynchronous function called "run". Here, we'll greet the user with a bold blue message, welcoming them to our currency converter. Using the inquirer library, we'll prompt the user with our predefined questions and await their answers.

Once we have the answers, we'll destructure them into the "from", "to", and "amount" variables. We'll then call the "convertCurrency" function, passing in these variables, and await the result. Finally, we'll display the conversion result to the user in a nicely formatted message, using chalk to add some vibrant colors.

To wrap things up, we'll call the "run" function, which kicks off our currency converter application. Once executed, you'll be able to select the currencies, enter the amount to convert, and instantly see the converted amount in the desired currency.

Congratulations! You've successfully built a currency converter using JavaScript. Feel free to explore further enhancements, such as adding more currencies, error handling, or even building a user interface for a more interactive experience.

That concludes our tutorial. We hope you found it helpful and enjoyable. Happy coding!
Cheers,
Bek Brace Team

Top comments (0)