JavaScript输出问题

时间:2014-03-08 14:37:16

标签: javascript output

我目前正在尝试获得一个程序来输出摊销表。该程序曾用于工作,因为它是以前的任务。但是,在那个特定的任务中,我只是将“结果”作为一个段落返回,其中包含贷款和表格的详细信息。由于警告框输入了变量。

现在我们正试图通过文本框获取输入,我正在尝试将表和信息输出到。问题是,我没有输出,我不能为我的生活找到出错的地方。真正的任务是进行输入检查,所以至少可以说这是令人沮丧的,因为我只是试图在此时获取作业。任何帮助将不胜感激......

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>index</title>
        <meta name="author" content="Christopher" />
        <script src="scripts.js"> </script>
        <!-- Date: 2014-03-06 -->
    </head>
    <body>

    <form>
        <fieldset>
            <legend>
                <h3>Inputs:</h3>
            </legend>
            Loan Amount: <input type="text" id="principal" placeholder="10000" />
            <p>
            Interest Rate: <input type="text" id="interest" placeholder="5.5" />
            <p>
            Terms: <select id="selector">
                <option value="12">12 months</option>
                <option value="24">24 months</option>
                <option value="36">36 months</option>
                <option value="48">48 months</option>
                <option value="60">60 months</option>               
            </select>   
            <p>
            <input type="button" id="myButton" value="Calculate" onclick="amort()" />       
        </fieldset>
        <fieldset>
            <legend>
                <h3>Outputs:</h3>
            </legend>
            <div id="outputArea"> </div>
        </fieldset>
    </form>


    /**
 * @author Christopher
 */
function amort(balance, interestRate, terms)
{
    var principal = document.getElementById("principal").value;
    var interestRate = document.getElementById("interest").value;
    var terms = document.getElementById("selector").value;
    var monthlyRate = interestRate/12;
    var payment = balance * (monthlyRate/(1-Math.pow(
        1+monthlyRate, -terms)));
    var result = "Loan amount: $" + balance.toFixed(2) +  "<br />" + 
        "Interest rate: " + (interestRate*100).toFixed(2) +  "%<br />" +
        "Number of months: " + terms + "<br />" +
        "Monthly payment: $" + payment.toFixed(2) + "<br />" +
        "Total paid: $" + (payment * terms).toFixed(2) + "<br /><br />";

    result += "<table border='1'><tr><th>Month</th><th>Balance</th>" + 
        "<th>Interest</th><th>Principal</th>";

    // insert your code here
    i=1;
    var balance = principal.toFixed(2);
    var interestPaid = balance * monthlyRate;
    var principalPaid = (payment - interestPaid).toFixed(2);
    //var totalPayment = interestPaid + principal;

    while (i<=terms){
        result+= "<tr><td>" + i + "</td><td>" + balance + "</td><td>"
         + interestPaid.toFixed(2) +"</td><td>" + principalPaid + "</td></tr>";
         var interestPaid = balance * monthlyRate;
         var principalPaid = (payment - interestPaid).toFixed(2);
         var balance = balance - interestPaid;
         var balance = (balance - principalPaid).toFixed(2);



        i++;
    } 
    result += "</table>";
    document.getElementById("outputArea").innerHTML = result;
}

2 个答案:

答案 0 :(得分:0)

您可以检查浏览器控制台中的错误。

删除参数,小心.toFixednumber的一种方法,因此您必须解析您的值。

function amort()
{
    var principal =  parseFloat(document.getElementById("principal").value);
    var interestRate =  parseFloat(document.getElementById("interest").value);
    var terms = document.getElementById("selector").value;
    var balance = principal.toFixed(2);
    var monthlyRate = interestRate/12;
    var payment = balance * (monthlyRate/(1-Math.pow(1+monthlyRate, -terms)));


    var result = "Loan amount: $" + balance +  "<br />" + 
        "Interest rate: " + (interestRate*100).toFixed(2) +  "%<br />" +
        "Number of months: " + terms + "<br />" +
        "Monthly payment: $" + payment.toFixed(2) + "<br />" +
        "Total paid: $" + (payment * terms).toFixed(2) + "<br /><br />";

    result += "<table border='1'><tr><th>Month</th><th>Balance</th>";
    result +=    "<th>Interest</th><th>Principal</th>";

    // insert your code here
    i=1;

    var interestPaid = balance * monthlyRate;
    var principalPaid = (payment - interestPaid).toFixed(2);
    //var totalPayment = interestPaid + principal;

    while (i<=terms){
        result+= "<tr><td>" + i + "</td><td>" + balance + "</td><td>"
         + interestPaid.toFixed(2) +"</td><td>" + principalPaid + "</td></tr>";
         var interestPaid = balance * monthlyRate;
         var principalPaid = (payment - interestPaid).toFixed(2);
         var balance = balance - interestPaid;
         var balance = (balance - principalPaid).toFixed(2);

        i++;
    } 
    result += "</table>";
    document.getElementById("outputArea").innerHTML = result;
}

FIDDLE

答案 1 :(得分:0)

如果您只是想获得输出以进行调试,那么您可以尝试

WScript.Echo( terms );
WScript.Echo( payment );

等等。