Logo LIBR
Halo

Portfolio

Back to Blog
9 min read
Challenge Day 3June 9, 2025

Interactive Calculator

JavaScript, Functions, Calculator
Syifa Fauziyah Arizal

Today I learned how to build a simple interactive calculator in JavaScript while improving input validation, code structure, and user interaction.

Today I worked on a simple interactive calculator. The challenge was not only about performing calculations, but also about handling user input correctly and making the calculator flexible enough to support multiple operations.

My first attempt looked like this:

JavaScript
const num1 = prompt("Enter first number");
const num2 = prompt("Enter second number");
const num1 = prompt("Enter operator (+, -, *, /)");

Looking back, there were already a few mistakes. I reused the variable name num1, and the operator input was not stored correctly.

Strings vs Numbers

Another problem appeared when I started doing calculations. Values returned by prompt() are strings, not numbers.

Number Conversion
const number1 = parseFloat(num1);
const number2 = parseFloat(num2);

let result;

Using parseFloat() allows the calculator to work with decimal numbers, making it more flexible than parseInt().

"2" + "3" = "23"
2 + 3 = 5

Refactoring the Calculator

After fixing the input conversion issue, I implemented the calculation logic using conditional statements.

If Else Implementation
const num1 = prompt("Enter first number");
const num2 = prompt("Enter second number");
const operator = prompt("Enter operator (+, -, *, /)");

const number1 = parseFloat(num1);
const number2 = parseFloat(num2);

let result;

if (operator === "+") {
  result = number1 + number2;
} else if (operator === "-") {
  result = number1 - number2;
} else if (operator === "*") {
  result = number1 * number2;
} else if (operator === "/") {
  result = number1 / number2;
}

This version worked, but the code could still be improved to make it cleaner and easier to extend.

Why do we use parseFloat()?

I wanted the calculator to support decimal numbers too.

Conversion Example
const number1 = parseFloat(num1);
const number2 = parseFloat(num2);

Unlike parseInt(), which removes the decimal part, parseFloat() preserves it. That means values like 10.5 can still be calculated correctly.

Input Validation

To make the calculator more reliable, I added validation before performing any operation.

Validation Check
if (isNaN(number1) || isNaN(number2)) {
  console.log("Input is not a valid number!");
}

This prevents unexpected results when users enter letters, symbols, or empty values.

Switch Case Version

The calculator became cleaner after replacing multiple if-else statements with a switch statement.

Switch Statement
switch(operator) {
  case "+":
    result = number1 + number2;
    break;

  case "-":
    result = number1 - number2;
    break;

  case "*":
    result = number1 * number2;
    break;

  case "/":
    result = number2 === 0
      ? "Error: division by zero"
      : number1 / number2;
    break;
}

Function and Looping

As a final improvement, I separated the calculation logic into a function and added a loop so users could perform multiple calculations without refreshing the page.

Function + Loop
let continueCalc = true;

while (continueCalc) {

  function calculate(num1, num2, operator) {
    switch (operator) {
      case "+": return num1 + num2;
      case "-": return num1 - num2;
      case "*": return num1 * num2;
      case "/":
        return num2 === 0
          ? "Error: division by zero"
          : num1 / num2;
      default:
        return "Operator not recognized";
    }
  }

  continueCalc = confirm("Want to calculate again?");
}

This approach makes the calculator easier to maintain because the calculation logic is stored in a single reusable function.

Key Takeaways

Converting input using parseFloat(), validating data with isNaN(), and separating logic into functions makes programs easier to maintain. Adding loops also improves the user experience by allowing repeated calculations without restarting the application.

More to read