我正在尝试解决一个作业问题,该问题要求我计算抵押贷款的付款,但是当我输入抵押金额时,例如:500000,期限为30年,它将直到0才更新每月付款。显示摊销时间表的javaScript应用程序。该应用程序将提示用户输入贷款金额(无逗号)。该应用程序将提示用户输入学年(30或15)。将会以5.75%的利息计算并显示每月付款。


  提示:
  
  您可以在上周修改抵押贷款申请。
  
  将利率设置为0.0575
  
  计算每月付款的公式为(((I / 12) * P) / (1-(Math.pow (1+ (I / 12), (Y * -12))))).toFixed(2);
  
  I =利率
  
  P =贷款本金(贷款金额)
  
  Y =学年
  
  总利息金额的公式为monthly payment x term Months - loan amount
  
  贷款总额的公式为loan amount + total interest
  
  抵押贷款余额的公式为total loan - monthly payment
  
  用于循环显示付款和余​​额
  
  使用if语句打印“ Ending ....”
  
  使用.toFixed(2)转换数字以仅保留两位小数


var I = 0.0575;
var LoanAmount = parseFloat(prompt("Enter the loan amount:",0));
var NumOfYears = parseInt(prompt("Enter the term years:",0));

var MortgageLoanBalance;
//var TermMonth = ;
var MonthlyPayment;


MonthlyPayment = (((I / 12) * LoanAmount) / (1- (Math.pow (1 + (I / 12), (NumOfYears * -12))))).toFixed(2);
var TotalInterestAmount = MonthlyPayment * (NumOfYears * 12) - LoanAmount;
var TotalLoan = LoanAmount + TotalInterestAmount;
MortgageLoanBalance = TotalLoan - MonthlyPayment;
//alert(MortgageLoanBalance);

if (MortgageLoanBalance > 0 ) {
    document.write("Your " + NumOfYears + " year mortgage amount is " + MonthlyPayment + " per month." );

//else if (MortgageLoanBalance > 0)
    document.write(TotalInterestAmount);
    document.write(TotalLoan);
    document.write(MortgageLoanBalance);

    for (i = 0; i <= MonthlyPayment; i++) {
        document.write("Month " + i + ": Your Monthly Payment " + MonthlyPayment + " and Mortgage loan balance " + MortgageLoanBalance + "<br>");
    }
} else {
    document.write("You have no payments");
  //document.write("I don't know what you did.")
}


应该打印出5个输出:

抵押期限(年)
抵押利率
抵押金额
总利息金额
总抵押金额

我没有收到任何错误消息,但它没有更新任何每月付款,看起来像是一团糟。

输出:

Your 15 year mortgage amount is 4.15 per month.247.0000000000001747.0000000000001742.8500000000001Month 0: Your Monthly Payment 4.15 and Mortgage loan balance 742.8500000000001
Month 1: Your Monthly Payment 4.15 and Mortgage loan balance 742.8500000000001
Month 2: Your Monthly Payment 4.15 and Mortgage loan balance 742.8500000000001
Month 3: Your Monthly Payment 4.15 and Mortgage loan balance 742.8500000000001
Month 4: Your Monthly Payment 4.15 and Mortgage loan balance 742.8500000000001

最佳答案

我在这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 = m - MonthlyPayment;
if (m <= 0) {
    document.write("This is the Ending Amortization Calculator.....");
}
}

09-25 17:51