Close
java

How to Make a Calculator with JavaScript?

How to Make a Calculator with JavaScript?
function decimal() {
var num1 = document.getElementById("num1").value;
var num2 = document.getElementById("num2").value;
if (num1.indexOf(".") == -1) {
document.getElementById("num1").value += ".";
}
if (num2.indexOf(".") == -1) {
document.getElementById("num2").value += ".";
}
}

function error() {
var num1 =parseFloat(document.getElementById("num1").value);
var num2 = parseFloat(document.getElementById("num2").value);
if (isNaN(num1) || isNaN(num2)) {
alert("Please enter valid numbers");
}
else if (num2 == 0) {
alert("Cannot divide by zero");
}
}

function backspace() {
var num1 = document.getElementById("num1").value;
var num2 = document.getElementById("num2").value;
document.getElementById("num1").value = num1.slice(0, -1);
document.getElementById("num2").value = num2.slice(0, -1);
}

Styling the Calculator

To make our calculator look more attractive, we can add some CSS styles. We can customize the colors, fonts, and sizes of the buttons and input fields.

cssform {

Leave a Reply

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