我创建了一个计算器来显示贷款期限内的还款额。

我设法计算出每个月的还款,利息,剩余贷款,但是我试图将其输出到表格中。列将是固定数目(5),但行应根据月数动态更新。

我看过几篇类似的文章,但无法使其适用于我的代码。

下面和jsfiddle中的代码

的HTML

<div class="mortgageInput">
  <form method="POST" name="calc" onclick="validateForm();repayment();return false;">
    <label>Amount </label>
    <input type="textbox" id="loan" value="100000"><br>
    <label>Years</label>
    <input type="textbox" id="years" value="15"><br>
    <label>Rate (%)</label>
    <input type="textbox" id="rate" value="6.00" onkeyup="calculate"><br>
    <input type="button" value="Calculate" id="btn"><br>
    <label>Monthly Repayment</label>
    <input type="textbox" id="monthlyRepayment"><br>
    <label>Monthly Interest Only</label>
    <input type="textbox" id="interest"><br>
    <label>Monthly Capital Repayment</label>
    <input type="textbox" id="capitalRepayment"><br>
    <label>Total Interest</label>
    <input type="textbox" id="totalInterest">
  </form>
</div>
 <br>
    Output into table...<p id="demo"></p>


JS

(document).on("keyup", calculate());

function validateForm(){
  var validation = true;
  validation &= calculate();
  validation &= pmt();
  return validation;
}

function calculate() {
  var p = document.querySelector("#loan").value;
  var y = document.querySelector("#years").value;
  var rate = document.querySelector("#rate").value;
  var r = rate / 100 / 12;
  var n = y * 12;

  var I = (p * r);
  var monthlyPayment = -pmt(r,n,p);
  var mr = (monthlyPayment - I);
  var ti = (monthlyPayment) * n - p;
  var list = JSON.stringify((computeSchedule(p, rate, 12, y, monthlyPayment)), 0, 4);

  document.querySelector("#interest").value = I.toFixed(2);
  document.querySelector("#totalInterest").value = ti.toFixed(2);
  document.querySelector("#capitalRepayment").value = mr.toFixed(2);
  document.querySelector("#monthlyRepayment").value = monthlyPayment.toFixed(2);
  document.getElementById("demo").innerHTML = list;
}

function pmt(rate,nper,pv) {
    var pvif, pmt;

    pvif = Math.pow( 1 + rate, nper);
    pmt = rate / (pvif - 1) * -(pv * pvif);

    return pmt;
}

function computeSchedule(loan_amount, interest_rate, payments_per_year,     years, payment) {
    var schedule = [];
    var remaining = loan_amount;
    var number_of_payments = payments_per_year * years;

    for (var i=0; i<=number_of_payments; i++) {
        var interest = remaining * (interest_rate/100/payments_per_year);
        var principle = (payment-interest);
        var row = [i, payment, interest>0?interest:0, principle>0?principle:0, remaining>0?remaining:0];
        schedule.push(row);
        remaining -= principle
    }

    return schedule;
}

最佳答案

上面的答案是正确的,但是如果您对性能有疑问,请在循环外插入html

var list = computeSchedule(p, rate, 12, y, monthlyPayment);
var tables = "";
for (var i = 0; i < list.length; i++) {
  tables += "<tr>" +
    "<td>" + list[i][0] + "</td>" +
    "<td>" + list[i][1] + "</td>" +
    "<td>" + list[i][2] + "</td>" +
    "<td>" + list[i][3] + "</td>" +
    "<td>" + list[i][4] + "</td>" +
    "</tr>";
}
document.getElementById("demo").innerHTML = '<table>' + tables + '</table>';

10-06 06:45