猿问

抵押摊销支付计算

我正在尝试解决一个作业问题,该问题要求我计算抵押贷款的付款,但是当我输入抵押金额时,例如: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


使用 for 循环显示付款和余额


使用 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 个输出:


按年

按揭 贷款利率

按揭金额

总利息金额

总按揭金额


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

Helenr
浏览 217回答 1
1回答

慕桂英3389331

我在这 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)&nbsp; / (1- (Math.pow (1+ (I / 12),&nbsp;(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) +&nbsp;"<br>");document.write("Your total mortgage amount: $" + TotalLoan.toFixed(2) + "<br>");&nbsp;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) +&nbsp; &nbsp;"<br>");document.write("<br>")&nbsp; &nbsp; m -= MonthlyPayment;if (m <= 0) {&nbsp; &nbsp; document.write("This is the Ending Amortization Calculator.....");&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答