我正在开发一个功能,该功能可以在我使用“提示”获取输入时进行基本的贷款摊销。从那个到使用表格,有些事情不太对劲。我正在尝试将同一张表打印到 div 区域,但目前根本无法获得它。我敢肯定这只是我的菜鸟眼睛,但我看不出我在这个上哪里错了....
感谢您花时间帮助平民。
html -
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Loan Amortization</title>
<script type="text/javascript" src="LoanAmortization.js"></script>
</head>
<body>
<h1>Loan Amortization</h1>
<fieldset>
<legend>Inputs</legend>
<form>
Principal: <input type="text" id="principalInput" /><br />
Interest: <input type="text" id="interestInput" /><br />
Terms: <select id="termsInput">
<option>12 months</option>
<option>24 months</option>
<option>36 months</option>
<option>48 months</option>
<option>60 months</option>
</select> <br />
<input type="button" id="myButton" value="Click this!"
onclick="amort('principalInput','interestInput','termsInput',
'myDiv')" />
</form>
</fieldset>
<fieldset>
<legend>Outputs</legend>
<div id="myDiv">
</div>
</fieldset>
</body>
JS-
function amort(principalInput, interestInput, termsInput)
{
total = parseInt(document.getElementById(principalInput).value);
var monthlyRate = parseFloat
(document.getByElementId(interestInput).value)/12;
var payment = total * (monthlyRate/(1-Math.pow(
1+monthlyRate, -terms)));
var result = "Loan amount: $" + principalInput.toFixed(2) + "<br />" +
"Interest rate: " + (interestInput*100).toFixed(2) + "%<br />" +
"Number of months: " + termsInput + "<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
var i = 1;
var interestPaid = (total * monthlyRate);
var principalPaid = payment - interestPaid;
while(i <= terms){
result+= "<tr><td>" + i + "</td>";
result+= "<td>" + total.toFixed(2) + "</td>";
result+= "<td>" + interestPaid.toFixed(2) + "</td>";
result+= "<td>" + principalPaid.toFixed(2) + "</td>";
result+= "</tr>";
total = total - principalPaid;
interestPaid = total * monthlyRate;
principalPaid = payment - interestPaid;
i++;
}
result += "</table>";
document.getElementById('myDiv').innerHTML = result;
}