NaN JavaScript函数

时间:2013-11-28 11:09:35

标签: javascript calculator nan

我一整天都在试图弄清楚这个愚蠢的错误。有谁能够帮我?我正在为我的编程课在JavaScript中创建一个贷款计算器。由于某种原因,我的“computePayoff”函数返回“NaN”错误,我不知道为什么。我认为它在第65行。有人可以帮忙吗?我很喜欢编程(显然),很多人都提前感谢。

<!DOCTYPE html>
<html>
<head>

<meta charset="utf-8">
<title>Loan Calculator</title>

<!--Input
        The user enters the required information and clicks on "Compute Payment"
    Processing
        The computer calls the correct function depending on the button pressed
    Output
        The monthly payment and/or the total payoff cost is sent to the screen.
-->

<script>
// This function is called when the "Compute Payment" button is clicked
function calculatePayment() {
    // Pulling values from the HTML fields and placing them into variables
    var principal = parseFloat(document.getElementById('principal').value);
    var annualRate = parseFloat(document.getElementById('interestrate').value);
    var years = parseFloat(document.getElementById('years').value);
    var periodsPerYear = parseFloat(document.getElementById('paymentsperyear').value);
    var numberOfPaymentsPaidToDate = parseFloat(document.getElementById('paymentstodate').value);

    // Calling another function to do the math calculations,
    // and then outputs it to an HTML form
    var paymentFinal = computePayment(principal, annualRate, years, periodsPerYear);
    document.getElementById("monthlypayment").value = paymentFinal;
}

function computePayment(principal, annualRate, years, periodsPerYear) {
    // All this math is to solve the equation: f = a(1+r)^n
    var rate = (annualRate / periodsPerYear) / 100;
    var lifeOfLoan = (years * periodsPerYear);
    var exponent = ((years * periodsPerYear) * -1);
    var topHalf = (principal * rate);
    var bottomHalf = 1 - (Math.pow((1 + rate), exponent));

    // The payment information is sent back to the "paymentFinal" variable
    var payment = "$" + (topHalf/bottomHalf).toFixed(2);
    return payment;
}

function calculatePayoff () {
    // Pulling values from the HTML fields and placing them into variables
    var principal = parseFloat(document.getElementById('principal').value);
    var annualRate = parseFloat(document.getElementById('interestrate').value);
    var years = parseFloat(document.getElementById('years').value);
    var periodsPerYear = parseFloat(document.getElementById('paymentsperyear').value);
    var numberOfPaymentsPaidToDate = parseFloat(document.getElementById('paymentstodate').value);

    // Calling another function to do the math calculations,
    // and then outputs it to an HTML form
    var payoffFinal = computePayoff(principal, annualRate, years, periodsPerYear, numberOfPaymentsPaidToDate);
    document.getElementById("totalpayoff").value = payoffFinal;
}

function computePayoff(principal, annualRate, years, periodsPerYear, numberOfPaymentsPaidToDate) {
    // All this math is to solve the equation: b = a(1+r)^n - p((1+r)^n - 1) / r
    var r = (annualRate / periodsPerYear) / 100; // Check!
    var n = numberOfPaymentsPaidToDate;
    var payments = computePayment(principal, annualRate, years, periodsPerYear); // Check!

    var payoff = (principal * (Math.pow((1 + r), n))) - (payments * (Math.pow((1 + r), n) - 1)) / r;
    return payoff;
}

    // This function is used to clear all the fields to blank
function resetForms() {
    document.getElementById('forms').reset();
}
</script>

</head>

<body>
<!-- Title -->
<h1>Loan Calculator</h1>
<hr>

<!-- I placed everything in a table so that it aligns correctly, 
and just looks a little bit cleaner and neater-->
<div>
<form id="forms">
    <table width="350px">
        <tr>
            <td>Amount Borrowed (Principal):</td>
            <td> <input type="text" id="principal" size="5"></td>
        </tr>
        <tr>
            <td>Annual Interest Rate (Example: 4.03%):</td>
            <td><input type="text" id="interestrate" size="3"></td>
        </tr>
        <tr>
            <td>Number of years:</td>
            <td><input type="text" id="years" size="2"></td>
        </tr>
        <tr>
            <td>Payments per year:</td>
            <td><input type="text" id="paymentsperyear" size="2"></td>
        </tr>
        <tr>
            <td><button type="button" onclick="calculatePayment()">Compute Payment</button></td>
            <td><input type="text" id="monthlypayment" size="5"></td>
        </tr>
        <tr>
            <td>Number of payments made to date:</td>
            <td><input type="text" id="paymentstodate" size="3"></td>
        </tr>
        <tr>
            <td><button type="button" onclick="calculatePayoff()">Total payoff amount</button></td>
            <td><input type="text" id="totalpayoff" size="7"></td>
        </tr>
        <tr>
            <td><input type="button" onclick="resetForms()" value="Reset"></td>
            <td></td>
        </tr>
    </table>
    <div id="output"></div>
</form>
</div>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

您的方法computePayments返回带有附加美元符号的值,因此将其设为字符串。这个computePayoff()在第65行失败。

所以将第41行改为:

var payment = (topHalf/bottomHalf).toFixed(2);

它会起作用