Interactive Calculator
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:
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.
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 = 5
Refactoring the Calculator
After fixing the input conversion issue, I implemented the calculation logic using conditional statements.
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.
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.
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(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.
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.
