我必须制定一个摊销时间表,其输出应为:
抵押期限(年)
抵押利率
抵押金额
总利息金额
总抵押金额

每月的每月抵押付款和抵押贷款余额。
如果余额为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循环会更好。我在这里所做的是在循环之前将balance设置为total_loan。我在余额之前结束循环-month_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......")
    

10-05 21:25