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.....");
}
}
添加回答
舉報