I made this calculator using JS, but it's not working:
var num1 = parseInt(document.getElementById("num1").value);
var num2 = parseInt(document.getElementById("num2").value);
function add() {
var result = num1 + num2;
document.getElementById("ans").innerHTML = result; }
function sub() {
var result = num1 - num2;
document.getElementById("ans").innerHTML = result; }
function mul() {
var result = num1 * num2;
document.getElementById("ans").innerHTML = result; }
function div() {
var result = num1 / num2;
document.getElementById("ans").innerHTML = result; }
body {
background-color: rgb(209, 100, 100);
}
h1 {
color: black;
text-align: center;
display: flex;
justify-content: center;
font-family: 'Arial Narrow Bold', sans-serif;
font-size: 100px;
}
.num1 {
color: white;
background-color: rgba(173, 216, 230, 0.681);
text-align: center;
display: flex;
justify-content: center;
font-family: 'Arial Narrow Bold', sans-serif;
font-size: 40px;
float: left;
padding: 20px;
margin: 45px;
border-radius: 10px;
}
.num2 {
color: white;
background-color: rgba(173, 216, 230, 0.681);
text-align: center;
display: flex;
justify-content: center;
font-family: 'Arial Narrow Bold', sans-serif;
font-size: 40px;
float: right;
padding: 20px;
margin: 45px;
border-radius: 10px;
}
.add {
color: white;
background-color: darkgray;
text-align: center;
display: flex;
justify-content: center;
font-family: 'Arial Narrow Bold', sans-serif;
font-size: 40px;
width: 200px;
height: 54px;
position: relative;
top: 235px;
right: 235px;
}
.sub {
color: white;
background-color: darkgray;
text-align: center;
display: flex;
justify-content: center;
font-family: 'Arial Narrow Bold', sans-serif;
font-size: 40px;
width: 200px;
height: 54px;
position: relative;
top: 100px;
left: 50px;
}
.mul {
color: white;
background-color: darkgray;
text-align: center;
display: flex;
justify-content: center;
font-family: 'Arial Narrow Bold', sans-serif;
font-size: 40px;
width: 200px;
position: relative;
top: 200px;
left: 50px;
}
.div {
color: white;
background-color: darkgray;
text-align: center;
display: flex;
justify-content: center;
font-family: 'Arial Narrow Bold', sans-serif;
font-size: 40px;
width: 200px;
position: relative;
top: 75px;
left: 330px;
}
h2 {
text-align: center;
display: flex;
justify-content: center;
font-size: 70px;
position: relative;
top: 100px;
left: 10px;
font-family: 'Arial Narrow Bold', sans-serif;
color: black;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Calculator</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>Calculator</h1>
<input type="number" name="First Nummber" class="num1" id="firstNum">
<input type="number" name="Second Nummber" class="num2" id="secondNum">
<button onclick="add()" class="add">Add</button>
<button onclick="sub()" class="sub">Subtract</button>
<button onclick="mul()" class="mul">Multiply</button>
<button onclick="div()" class="div">Divide</button>
x<h2 id="ans"></h2>
</body>
</html>
When I enter values and click on an operation, it just shows NaN
How do I fix this?
I realize this has something to do with the parseInt function but please specify what is going wrong.