我正在尝试进行while循环,但是循环是无限的,我不确定为什么

时间:2019-04-29 02:36:14

标签: javascript

我正在构建此代码,并且在对其进行测试之后,该代码无法正常工作,并且冻结了网页,我认为这是由于无限循环所致。我不确定是什么问题。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="ISO-8859-1">
        <title>Student Loan Payoff</title>
        <script type="text/javascript">
function DisplayPayoffSchedule() {
    var amount, ir, mp, monthcounter;

    amount = parseFloat(document.getElementById('loanBox').value);
    ir = parseFloat(document.getElementById('rateBox').value);
    mp = parseFloat(document.getElementById('paymentBox').value);

    document.getElementById('scheduleDiv').innerHTML = 'Original loan amount: ' + amount + '<br>';

    monthcounter = 0;

    while (amount > mp) {
        amount = (1 + (ir / 12)) * amount - mp;
        monthcounter++;
        document.getElementById('scheduleDiv').innerHTML += ' Month ' + monthcounter + ': Amount Remaining : ' + amount + '<br>';
    }

}
        </script>
    </head>
<body>
    <p>
Amount of Loan: <input type="text" id="loanBox" size="6"><br>
Annual Interest Rate: <input type="text" id="rateBox" size="6"><br>
Monthly Payment: <input type="text" id="paymentBox" size="6">
    </p>
        <input type="button" value="Display Payoff Schedule"  onclick="DisplayPayoffSchedule();">
    <hr>
        <div id="scheduleDiv"></div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

由于以下原因导致的错误

amount = (1 + (ir / 12)) * amount - mp;

如果您输入“每月付款”为负数,则该金额始终大于mp,且无穷循环。

应防止输入负数。

当monthcounter太大时,也应该中断。

if(monthcounter > 50){
    break;
}

由于贷款额太大而利率和月还款额太小,它将重复很多循环,看起来就像是无休止的循环。

function DisplayPayoffSchedule() {
    var amount, ir, mp, monthcounter;

    amount = parseFloat(document.getElementById('loanBox').value);
    ir = parseFloat(document.getElementById('rateBox').value);
    mp = parseFloat(document.getElementById('paymentBox').value);

    if(mp < 0){
    alert('Monthly Payment must be positive');
    return;
    }

    document.getElementById('scheduleDiv').innerHTML = 'Original loan amount: ' + amount + '<br>';

    monthcounter = 0;

    while (amount > mp) {
        amount = (1 + (ir / 12)) * amount - mp;
        monthcounter++;
        document.getElementById('scheduleDiv').innerHTML += ' Month ' + monthcounter + ': Amount Remaining : ' + amount + '<br>';
        if(monthcounter > 50){
           break;
        }
    }

}

<!DOCTYPE html>
<html>
    <head>
        <meta charset="ISO-8859-1">
        <title>Student Loan Payoff</title>
        <script type="text/javascript">
function DisplayPayoffSchedule() {
    var amount, ir, mp, monthcounter;

    amount = parseFloat(document.getElementById('loanBox').value);
    ir = parseFloat(document.getElementById('rateBox').value);
    mp = parseFloat(document.getElementById('paymentBox').value);
	
	if(mp < 0){
	alert('Monthly Payment must be positive');
	return;
	}

    document.getElementById('scheduleDiv').innerHTML = 'Original loan amount: ' + amount + '<br>';

    monthcounter = 0;

    while (amount > mp) {
        amount = (1 + (ir / 12)) * amount - mp;
        monthcounter++;
        document.getElementById('scheduleDiv').innerHTML += ' Month ' + monthcounter + ': Amount Remaining : ' + amount + '<br>';
        if(monthcounter > 50){
           break;
        }
    }

}
        </script>
    </head>
<body>
    <p>
Amount of Loan: <input type="text" id="loanBox" size="6"><br>
Annual Interest Rate: <input type="text" id="rateBox" size="6"><br>
Monthly Payment: <input type="text" id="paymentBox" size="6">
    </p>
        <input type="button" value="Display Payoff Schedule"  onclick="DisplayPayoffSchedule();" />
    <hr>
        <div id="scheduleDiv"></div>
</body>
</html>