Open In App

How to Create a CLI Based Calculator App using Node.js ?

Last Updated : 31 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Creating a Command-Line Interface (CLI) based calculator app using Node.js is a great way to get hands-on experience with Node.js and enhance your understanding of JavaScript in a server-side context. In this article, we will walk through the steps to build a simple yet functional calculator that can perform basic arithmetic operations such as addition, subtraction, multiplication, and division.

Approach

To create a CLI based calculator app we are going to use the process object, process object contains various properties in which we will use the arguments property. This will helps to fetch the arguments passed in the command line and then we will loop through every single element and perform the different operations.

Functionalities

In this article, we are going to create a simple application where we can add or subtract numbers using the command line argument:

The below command will add the numbers num1 and num2 and print the result to the console.

node app.js add num1 num2

The below command will subtract the numbers num1 and num2 and print the result to the console.

node app.js subtract num1 num2

The below command will multiply the numbers num1 and num2 and print the result to the console.

node app.js multiply num1 num2

The below command will divide the numbers num1 and num2 and print the result to the console.

node app.js divide num1 num2

Steps to Create a CLI Calculator

Step 1: Initialize a Node.js Project

First, create a new directory for your project and initialize it with npm.

mkdir cli-calculator
cd cli-calculator
npm init -y

Step 2: Create a file app.js

Inside this file, we are going to write the code.

Step 3: Create a constant 'argvs'

Create a constant 'argvs' and set it to process.argv properties: This will contain the arguments that the user passes in the command line.

const argvs = process.argv

Step 4: Create another const 'argv' and set it to the 'argvs' with the slice method

Since the first two arguments passed in the command line are irrelevant to the program because the first one is 'node' itself since we use it to run the application and second is the name of the file which we want it to execute in our case its 'app.js', so we have to remove it by using slice method.

const argv = argvs.slice(2)

Step 5: Set the remaining arguments inside 'argv' array to different constants:

const operation = argv[0]
const operator1 = parseInt(argv[1])
const operator2 = parseInt(argv[2])

Step 6: Set if operator for Add

If the first contact which is the 'operation' value is added then we execute the following command and print the addition to the terminal using the console.log command.

if (operation === 'add') {
console.log(operation + ' is '
+ (operator1 + operator2));
}

Step 7: Set if operator for Subtract

If the first contact which is the 'operation' value is subtracted then we execute the following command and print the subtraction to the terminal using the console.log command.

if (operation === 'subtract') {
console.log(operation + ' is '
+ (operator1 - operator2));
}

Step 8: Set if operator for Multiplication

If the first contact which is the 'operation' value is multiplied then we execute the following command and print the multiplication to the terminal using the console.log command.

if (operation === 'multiply') {
console.log(operation + ' is '
+ (operator1 * operator2));
}

Step 9: Set if operator for Division

If the first contact which is the 'operation' value is divided then we execute the following command and print the division to the terminal using the console.log command.

if (operation === 'divide') {
console.log(operation + ' is '
+ (operator1 / operator2));
}

Example: Implementation to create a CLI based calculator app

Node
// app.js 

const argvs = process.argv
const argv = argvs.slice(2)
const operation = argv[0]
const operator1 = parseInt(argv[1])
const operator2 = parseInt(argv[2])

if (operation === 'add') {
    console.log(operation + ' is ' 
        + (operator1 + operator2));
}
if (operation === 'subtract') {
    console.log(operation + ' is ' 
        + (operator1 - operator2));
}
if (operation === 'multiply') {
    console.log(operation + ' is ' 
        + (operator1 + operator2));
}
if (operation === 'divide') {
    console.log(operation + ' is ' 
        + (operator1 - operator2));
}

Steps to run the program to achieve the desired output.

node app.js add 4 5

This will add the numbers 4 and 5 and print the result 9 to the console.

node app.js subtract 10 5

This will subtract the numbers 10 and 5 and print the result 5 to the console.

Output:


Next Article

Similar Reads