2016-06-15 90 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相同。

相關問題