Skip to content

Pratama Digital

Your Digital Partner

Menu
  • Home
  • Download Area
  • Pratama Digital Net
  • Internet Speed Test
  • About Us
Menu

Building a Simple Calculator Script with Node.js

Posted on September 20, 2023

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.

Share this:

  • Facebook
  • X

Related

Leave a Reply Cancel reply

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

Recent Posts

  • 10 Powerful Alternatives to MikroTik for Small to Enterprise Networks
  • LDAP: A Guide for Linux System Administrators
  • Virtual Private Server (VPS): A Guide for Beginners
  • Forward Ports on Your Router for Online Games
  • MikroTik RouterOS 6 vs. RouterOS 7: Key Differences, Advantages, and Disadvantages

Tags

24h clock adobe adobe cc adobe cs master almalinux arduino autodesk basic ip address cad software calculator php connect java dns server download photoshop ecmp formula ipv4 innodb install iptables ipsec iptables java mysql linux firewall linux mint linux security load balance microsoft office myisam mysql mysql engine node.js nodejs nth office alternative openvpn pcc php port game online port mikrotik postgresql postgresql to mysql public dns rocky linux running text script php ubuntu LTS wireguard

Categories

  • Advertising
  • CAD software
  • Computer Component
  • Control Panel
  • Database
  • DNS
  • Download
  • File Server
  • Firewall
  • Graphic Design
  • Internet
  • IP Address
  • Linux OS
  • Load Balance
  • Mikrotik
  • Movie
  • Novel/Book
  • Office
  • OpenWRT
  • Processor & GPU
  • Programming
  • Routing
  • Security
  • Server
  • Tutorial
  • Video Editing
  • VPN
  • Web Programming
  • Windows OS
  • Wordpress

About Us

We are a company specializing in network configuration, MikroTik and computer installation services, as well as wifi setup. With experience dating back to 2017, we have gained the trust of numerous clients from various regions who rely on our services. Our expertise extends to catering to personal, office, institutional, and industrial needs.

Archives

  • December 2024
  • September 2024
  • June 2024
  • May 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023

Recent Posts

  • 10 Powerful Alternatives to MikroTik for Small to Enterprise Networks
  • LDAP: A Guide for Linux System Administrators
  • Virtual Private Server (VPS): A Guide for Beginners
  • Forward Ports on Your Router for Online Games
  • MikroTik RouterOS 6 vs. RouterOS 7: Key Differences, Advantages, and Disadvantages
©2025 Pratama Digital | Design: Newspaperly WordPress Theme