如何调试JavaScript错误“Uncaught ReferenceError:number not defined”?

时间:2022-11-05 14:21:03

I've been coding a calculator but something doesn't work. I've been getting errors:

我一直在编写计算器,但有些东西不起作用。我一直在收到错误:

Uncaught ReferenceError: number is not defined

未捕获的ReferenceError:未定义数字

Here is my code:

这是我的代码:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
alert("Calculator by Pavel (Dont block these popups, this website uses them)")
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 answer is"+result);
}
</script>
</head>
<body>
<div>
<input id="a" type="text"/>
<input id="b" type="text"/>
<input type="button" onclick="sum()" value="sum"/>
</div>
</body>
</html>

3 个解决方案

#1


You have a mistake - JavaScript is case sensitive, so you must use Number

您有一个错误 - JavaScript区分大小写,因此您必须使用Number

a=Number(document.getElementById("a").value);

Reference

#2


<input id="a" type="text"/>
<input id="b" type="text"/>
<input id="1" type="button" onclick="sum()" value="sum"/>

<script type="text/javascript">
function sum()
{
    setValues();
    result= a+b;
    alert("The answer is"+result);
    // document.getElementById("ss").innerHTML = "Hello World";
    //  alert("hai");
}
    var a,b,result;
function setValues()
{

    a=Number(document.getElementById("a").value);
    b=Number(document.getElementById("b").value);

}
</script>

Fiddle link

#3


You can also use a=parseInt(...); in place of a=Number(); to take numbers only. Give it a Try!

你也可以使用a = parseInt(...);代替a = Number();仅取数字。试试看!

#1


You have a mistake - JavaScript is case sensitive, so you must use Number

您有一个错误 - JavaScript区分大小写,因此您必须使用Number

a=Number(document.getElementById("a").value);

Reference

#2


<input id="a" type="text"/>
<input id="b" type="text"/>
<input id="1" type="button" onclick="sum()" value="sum"/>

<script type="text/javascript">
function sum()
{
    setValues();
    result= a+b;
    alert("The answer is"+result);
    // document.getElementById("ss").innerHTML = "Hello World";
    //  alert("hai");
}
    var a,b,result;
function setValues()
{

    a=Number(document.getElementById("a").value);
    b=Number(document.getElementById("b").value);

}
</script>

Fiddle link

#3


You can also use a=parseInt(...); in place of a=Number(); to take numbers only. Give it a Try!

你也可以使用a = parseInt(...);代替a = Number();仅取数字。试试看!