简体   繁体   中英

When I click the sum, multiply and divide button I got “NaN” error

Here is my code:
 <!DOCTYPE html>
<HTML>
<HEAD>
<script>

var a,b,result;

function setValues()
{
a = Number(document.getElementById("a").Value);
b = Number(document.getElementById("b").Value);
}

function sum()
{
setValues();
result = a+b;
alert("The Sum For The Values Is "+result);
}

function multiply()
{
setValues();
result = a*b;
alert("The Product For The Values Is "+result);
}

function div()
{
setValues();
result = a/b;
alert("The Quotient For The Values Is "+result);
}

</script>

</HEAD>


<STYLE>
body
{
background-color: #b0c4de;
}

 </STYLE>

 <body>
 <CENTER>
 <H1><B><U> Fun Facts!</B></U></H1>
 <H4>Did You Know There Are Over 7 Billion People In The World!!!</H4>
 <div>
 <input Id="a" Type="Text"/>
 <input Id="b" Type="Text"/>
 <input Type="Button"  onclick="sum()" Value="sum"/>
 <input Type="Button"  onclick="multiply()" Value="multiply"/>
 <input Type="Button"  onclick="div()" Value="divide"/>
 </div>
 </body>



 </CENTER>






</HTML>

This is a simple calculator, but when i clicks um,multiply or divide, I get the "NaN error. I add the alert system, so the answer is the sum,product, or quotient and then the answer, but i DO GET THE ALERT SYSTEM, BUT THE RESULT i GET IS NaN Please help. thanks

  <!DOCTYPE html> <HTML> <HEAD> <script> var a,b,result; function setValues() { a = Number(document.getElementById("a").Value); b = Number(document.getElementById("b").Value); } function sum() { setValues(); result = a+b; alert("The Sum For The Values Is "+result); } function multiply() { setValues(); result = a*b; alert("The Product For The Values Is "+result); } function div() { setValues(); result = a/b; alert("The Quotient For The Values Is "+result); } </script> </HEAD> <STYLE> body { background-color: #b0c4de; } </STYLE> <body> <CENTER> <H1><B><U> Fun Facts!</B></U></H1> <H4>Did You Know There Are Over 7 Billion People In The World!!!</H4> <div> <input Id="a" Type="Text"/> <input Id="b" Type="Text"/> <input Type="Button" onclick="sum()" Value="sum"/> <input Type="Button" onclick="multiply()" Value="multiply"/> <input Type="Button" onclick="div()" Value="divide"/> </div> </body> </CENTER> </HTML> 

You have an simple mistake:

function setValues()
{
a = Number(document.getElementById("a").Value);
b = Number(document.getElementById("b").Value);
}

The .Value should be .value ...

Greetings from Austria

Working Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM