2016-06-15 6 views
-1

プログラムのボタンを押すたびに、最大呼び出しサイズを超えていると表示され、プログラムを実行するために何をすべきかわかりません。プログラムの最大呼び出しスタックが超過

ここにコードがあります。あなたのabs機能で

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_input)は再び関数を呼び出します。それで、再び起こります。そしてまた。そしてまた。そしてまた。そして...最終的に、コンピュータはあなたが何回それをどこから呼び出したかを覚えていません。

あなたはおそらく望んでいたことは、あなたのabs()関数内Math.abs(abs_input)を呼び出すことです - あなたは無限再帰(機能これ以上できないまで自身を呼び出しを得ることはありませんので、ごabs()ものとは異なる、組み込み関数)。

roundMath.roundlogMath.logと同じです。

関連する問題