DEV Community

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

Posted on • Edited on

8

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

SurveyJS custom survey software

JavaScript UI Library for Surveys and Forms

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

View demo

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay