我必须做一个摊销计划,输出应该是: 按揭年限 按揭利率 按揭金额 利息总额 总按揭金额
每个月的每月抵押贷款付款和抵押贷款余额。 如果余额为 0,则打印“这是期末摊销计算器……”
谢谢!
<script>
var I = 0.0575;
var total_interest;
var total_loan;
var balance;
var P = parseFloat(prompt("Enter the loan amount $:", 0));
var Y = parseInt(prompt("Enter the term years ('30 or 15'):", 0));
var termMonths = Y * 12;
//if(Y != "30" || Y != "15"){
// alert("Please enter the term years: '30 or 15'");
// var Y = parseInt(prompt("Enter the term years ('30 or 15'):",0));
var month_payment = (((I / 12) * P) / (1 - (Math.pow(1 + (I / 12), (Y * -12))))).toFixed(2);
total_interest = parseFloat((month_payment * termMonths) - P).toFixed(2);
total_loan = parseFloat(total_interest + P).toFixed(2);
document.write("Mortgage term in years:", +Y + "<br>");
document.write("Mortgage Interest Rate: " + I + "<br>");
document.write("Mortgage amount: $" + P + "<br>");
document.write("Total Interest Amount: $" + total_interest + "<br>");
document.write("Total Mortgage Amount: $" + total_loan + "<br><br>");
var numPayments = 12 * Y;
for (var i = 0; i <= numPayments; i++) {
balance = parseFloat(total_loan - month_payment).toFixed(2);
document.write("Monthly Mortgage Payments: $" + month_payment + " & Mortgage Loan Balance for each month: $" + balance + "<br>");
}
if (balance == 0) {
document.write("This is the Ending Amortization Calculator......")
}
</script>
最佳答案
问题是您没有初始化余额。此外,对于这种情况,while 循环会更好。我在这里所做的是在循环之前将余额设置为 total_loan。我在余额 - monthly_payment < 0 之前结束循环,在脚本末尾我写下余额为 0.00 美元。
var I = 0.0575;
var total_interest;
var total_loan;
var balance;
var P = parseFloat(prompt("Enter the loan amount $:",0));
var Y = parseInt(prompt("Enter the term years ('30 or 15'):",0));
var termMonths = Y * 12;
//if(Y != "30" || Y != "15"){
// alert("Please enter the term years: '30 or 15'");
// var Y = parseInt(prompt("Enter the term years ('30 or 15'):",0));
var month_payment = (((I / 12) * P) / (1- (Math.pow (1+ (I / 12),(Y * -12))))).toFixed(2);
total_interest = parseFloat((month_payment * termMonths)-P).toFixed(2);
total_loan = parseFloat(total_interest + P).toFixed(2);
document.write("Mortgage term in years:", + Y +"<br>");
document.write("Mortgage Interest Rate: " + I +"<br>");
document.write("Mortgage amount: $" + P +"<br>");
document.write("Total Interest Amount: $" + total_interest +"<br>");
document.write("Total Mortgage Amount: $" + total_loan +"<br><br>");
var numPayments = 12 * Y;
balance = total_loan
while (balance - month_payment > 0){
balance = parseFloat(balance - month_payment).toFixed(2);
document.write("Monthly Mortgage Payments: $" + month_payment + " & Mortgage Loan Balance for each month: $" + balance +"<br>");
}
document.write("Monthly Mortgage Payments: $" + month_payment + " & Mortgage Loan Balance for each month: $0.00" +"<br>");
document.write("This is the Ending Amortization Calculator......")
关于javascript - 为什么 total_loan 不起作用(与 total_interest 相同),以及如何正确显示时间表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46285850/