Skip to main content

Command Palette

Search for a command to run...

Node.js Mini Calculator

Updated
2 min read

okay you can follow me for create mini calculator with nodejs

  1. first open your gitbash

  2. go to your folder

  3. create file input2.js and fill in the file as follows

  4. create file calculator and fill like this

    and this code for content file

     import { inputText, closeInput } from "./input2.js";
    
     console.clear();
     console.log("  Calculator Console  ");
    
     let isRunning = true;
    
     while (isRunning) {
      console.log("\nChoose Operation:");
      console.log("1. addition");
      console.log("2. Subtraction");
      console.log("3. Multiplication");
      console.log("4. Division");
      console.log("0. Exit");
    
      const choice = await inputText("Enter a Choice (0-4)");
      switch (choice) {
       case "0": {
        isRunning = false;
        console.log("Thank you, Program finished.");
        break;
       }
       case "1":
       case "2":
       case "3":
       case "4": {
         const a = Number(await inputText("Enter the first number "));
         const b = Number(await inputText("Enter the second number "));
         switch (choice) {
          case "1": {
           console.log(`Result: ${a} + ${b} = ${a + b}`);
           break;
          }
          case "2": {
           console.log(`Result: ${a} - ${b} = ${a - b}`);
           break;
          }
          case "3": {
           console.log(`Result: ${a} * ${b} = ${a * b}`);
          }
          case "4": {
           if (b === 0) {
            console.log("❌ cannot divide by 0");
           } else {
            console.log(`Result: ${a} / ${b} = ${a / b}`);
           }
           break;
          }
         }
        break;
       }
      default:
        console.log("Invalid choice, Please try again!");
      }
     }
    
  5. test your code or your program

    if you want to exit from program you can click ctrl + c

    you have left the program.

End !!

thank you, and follow me for more content about IT specifically backend.

More from this blog

The Digital Alchemist's Log

21 posts