2016-06-15 371 views
-1

每当我按下我的程序中的按钮,它说最大的调用大小已超出,我不知道如何做我的程序运行。程序最大调用堆栈超过

这是代码。

function check() { 
 
    var input = document.getElementById("num").value; 
 
    var check = /^[0-9]*$/; 
 
    check.test(input) ? window.alert("You have only entered numeric characters! Please proceed to press one of the other buttons.") : window.alert("Please enter only numbers!"); 
 
} 
 

 
function abs() { 
 
    var abs_input = document.getElementById("num").value; 
 
    document.write("The absolute value of your number is: " + abs(abs_input)); 
 
} 
 

 
function round() { 
 
    var input = document.getElementById("num").value; 
 
    document.write("The value of your number rounded is: " + round(input)); 
 
} 
 

 
function log() { 
 
    var input = document.getElementById("num").value; 
 
    document.write("The natural logarithm of your number is: " + log(input)); 
 
}
<center> 
 
    <label style="font-size:40px">Please Enter A Number Here:</label> 
 
    <input type="text" id="num" style="font-size:40px"> 
 
    <br /> 
 
    <br /> 
 
    <br /> 
 
    <br /> 
 
    <button type="button" onclick="check()" style="font-size:20px">Check</button> 
 
    <br /> 
 
    <br /> 
 
    <br /> 
 
    <br /> 
 
    <button type="button" onclick="abs()" style="font-size:20px">Absolute Value</button> 
 
    <br /> 
 
    <br /> 
 
    <br /> 
 
    <br /> 
 
    <button type="button" onclick="round()" style="font-size:20px">Rounded Value</button> 
 
    <br /> 
 
    <br /> 
 
    <br /> 
 
    <br /> 
 
    <button type="button" onclick="log()" style="font-size:20px">Log Value</button> 
 
    <br /> 
 
    <br /> 
 
</center>

+0

它因为无穷的递归。你能解释你想达到的目标吗? – Rajesh

回答

3

在你abs功能,abs(abs_input)会再次调用该函数。其中,它会再次发生。然后再次。然后再次。然后再次。而且......最终计算机将无法记住你曾多次调用它以及从哪里调用它。

你可能想要的东西是叫你Math.abs(abs_input)功能abs()里面 - 一个内置的功能,从不同的abs()一个,这样你就不会得到无限递归(一个函数调用自身直到它没有任何更多)。

roundMath.roundlogMath.log相同。

相关问题