2017-09-18 11 views
1

我要做的攤銷時間表和輸出應該是:在幾年 抵押期限 按揭利率 按揭量 總利息金額 按揭總額爲什麼total_loan不工作(是一樣的total_interest),也IDK如何顯示時間表正確

每月的按揭付款&按揭貸款餘額每月。 如果餘額爲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> 

回答

1

問題是您沒有初始化餘額。另外,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>"); 
 
\t  document.write("This is the Ending Amortization Calculator......") 
 

+0

謝謝你現在@jeron我知道了! 另外,你知道那條線有什麼問題嗎?因爲total_interest + P總和不起作用。 total_loan = parseFloat(total_interest + P).toFixed(2); –

+0

你確定它不起作用嗎?當我爲P投入500,000美元時,total_loan給我550429.60美元。你期望的數字是多少? – jeron

+0

因爲她給了我們這些公式: 總利息金額是月付款x期數月 - 貸款金額。 貸款總額+貸款總額。 因此,如果我的貸款額度是5萬美元,並且我在30年內每月支付300美元,那麼在幾個月內應該是300 *期 - 50,000是正確的? 然後總貸款應該是50,000 +總利息。 total_loan和total_interest的數量不能相同。我錯了嗎? –

相關問題