So, in my JavaScript web app calculator, the addition works great. But, when I try to code the subtraction calculator, everything goes right, except for when I try the calculator out, the addition works fine, but when I try to subtract two numbers, it just says: NaN Here's my calculator. The subtraction calculator begins at the paragraph that says "Subtraction Calculator!"
Here's my code:
<!DOCTYPE html>
<html>
<body>
<p>Addition Calculator!</p>
<input id="n1" value=""/>
<input id="n2" value=""/>
<button type="button" onclick="myFunction()">Submit</button>
<p id="answer"></p>
<script>
function myFunction() {
var n1=document.getElementById("n1").value;
var n2=document.getElementById("n2").value;
answer = ( Number(n1) + Number(n2) );
document.getElementById("answer").innerHTML=answer;
}
</script>
<p>Subtraction Calculator!</p>
<input id="sn1" value=""/>
<input id="sn2" value=""/>
<button type="button" onclick="subtract()">Submit</button>
<p id="sanswer"></p>
<script>
function subtract() {
var sn1=document.getElementById("sn1").value;
var sn1=document.getElementById("sn2").value;
stotal = ( Number(sn1) - Number(sn2) );
document.getElementById("sanswer").innerHTML=stotal;
}
</script>
</body>
</html>