1 回答
TA贡献2036条经验 获得超8个赞
我在这 3 条评论和我的朋友的帮助下编辑了我的代码,他在 javascript 方面更擅长我。我所做的主要事情是将 .toFixed(2) 函数添加到 2 个小数点以解决 JS 中的小数问题。我完全重新组织了变量,我像评论所说的那样将每月付款放在 for 循环中,主要问题是我没有为循环创建一个 var 以用于抵押贷款余额,我还将利息乘以100 等于一个百分比,差不多就是这样,它输出的正是我想要的。
var LoanAmount = parseFloat(prompt("Enter the loan amount:",));
var NumOfYears = parseInt(prompt("Enter the term years:",));
var MortgageLoanBalance;
var TotalLoan;
var TotalInterestAmount;
var MonthlyPayment;
var I;
I = 0.0575;
MonthlyPayment = (((I / 12) * LoanAmount) / (1- (Math.pow (1+ (I / 12),
(NumOfYears * -12)))));
TotalInterestAmount = MonthlyPayment * (NumOfYears * 12) - LoanAmount;
TotalLoan = LoanAmount + TotalInterestAmount;
MortgageLoanBalance = TotalLoan - MonthlyPayment;
document.write("Your mortgage term in years: " + NumOfYears + "<br>");
document.write("Your mortgage interest rate is: " + I * 100 + "% <br>");
document.write("Your mortgage loan amount: $" + LoanAmount.toFixed(2) + "<br>" );
document.write("Your total interest amount: $" + TotalInterestAmount.toFixed(2) +
"<br>");
document.write("Your total mortgage amount: $" + TotalLoan.toFixed(2) + "<br>");
document.write("<br>");
document.write("Your Monthly payments begins now for your " + NumOfYears + " years
" + "<br>");
document.write("<br>");
var m = MortgageLoanBalance;
for (m = MortgageLoanBalance; m >= 0; m--) {
document.write("Your Monthly Payment is: $" + MonthlyPayment.toFixed(2) + "<br>");
document.write("Your Mortgage loan balance this month is: $" + m.toFixed(2) +
"<br>");
document.write("<br>")
m -= MonthlyPayment;
if (m <= 0) {
document.write("This is the Ending Amortization Calculator.....");
}
}
添加回答
举报