Introduction

Node.js is a versatile JavaScript runtime that allows developers to build server-side applications. In this article, we’ll create a basic command-line calculator script using Node.js. This project will help you get acquainted with the fundamentals of JavaScript and how to use Node.js for simple command-line applications.

Prerequisites

Before you get started, ensure you have Node.js installed on your system. You can download it from the official website (https://nodejs.org/).

Creating the Calculator Script

  1. Initialize a Node.js ProjectCreate a new folder for your project and open it in your terminal. Run the following command to initialize a Node.js project:
    npm init -y

    This command creates a package.json file with default settings.

  2. Create a JavaScript FileInside your project folder, create a JavaScript file, for example, calculator.js.
  3. Open calculator.js and Start CodingIn calculator.js, you’ll write the code for your simple calculator. Here’s a basic structure to get you started:
    // Import the built-in ‘readline’ module for user input
    const readline = require(‘readline’);// Create an interface for reading input and displaying output
    const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
    });// Function to perform calculations
    function calculate() {
    rl.question(‘Enter an expression (e.g., 2 + 3): ‘, (input) => {
    try {
    const result = eval(input); // Use ‘eval’ to evaluate the expression
    console.log(`Result: ${result}`);
    } catch (error) {
    console.error(‘Invalid input. Please try again.’);
    }
    rl.close();
    });
    }// Start the calculator
    calculate();


    This code does the following:

    • It imports the readline module to handle user input.
    • Creates an interface (rl) for reading input and displaying output.
    • Defines a calculate function that reads a user’s expression, evaluates it using eval, and prints the result.
    • Calls calculate to start the calculator.
  4. Run the CalculatorOpen your terminal, navigate to the project folder, and run the script with Node.js:
    node calculator.js

    The calculator will prompt you to enter an expression (e.g., 2 + 3) and will display the result.

Testing the Calculator

You can test your calculator by trying various expressions like addition, subtraction, multiplication, and division. For example:

  • 2 + 3
  • 10 - 5
  • 6 * 4
  • 20 / 4

Your Node.js-based calculator should be able to handle these basic arithmetic operations.

Conclusion

In this article, you’ve created a simple calculator script using Node.js. This project demonstrates how to handle user input, perform calculations, and provide output via the command line. You can further enhance this calculator by adding error handling and additional features, making it a valuable learning experience for those new to Node.js and JavaScript programming.

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *