2017-08-07 46 views
1

我需要一個函數來計算抵押年金分期付款表,其中包括支付金額,本金,利息支付等行和列的輸出,按照如下鏈接按月排序: Output exampleJavaScript中的年金攤銷表

輸出文件格式應該是.cvs。目前,我堅持這個女巫還是遠離結果:

var i = 5/100; 
 
var loanAmount = 15000; 
 
var m = 12; 
 

 
var monthlyPayment = loanAmount*(i/12)*Math.pow((1+i/12), m)/(Math.pow((1+i/12), m)-1) 
 

 
var currentBalance = loanAmount; 
 
var paymentCounter = 1; 
 
var totalInterest = 0; 
 
monthlyPayment = monthlyPayment; 
 
\t \t 
 
while(currentBalance > 0) { 
 
    //this calculates the portion of your monthly payment that goes towards interest 
 
    towardsInterest = (i/12)*currentBalance; 
 
\t \t 
 
    if (monthlyPayment > currentBalance){ 
 
     monthlyPayment = currentBalance + towardsInterest; 
 
    } 
 
\t \t 
 
    towardsBalance = monthlyPayment - towardsInterest; 
 
    totalInterest = totalInterest + towardsInterest; 
 
    currentBalance = currentBalance - towardsBalance; 
 
}

會很感激有這方面的幫助。

+2

你想讓我們做什麼?爲你寫代碼? 你可以提到你被卡住的確切位置 –

回答

0

嘗試這樣:

const annuity = (C, i, n) => C * (i/(1-(1+i)**(-n))); 

const balance_t = (C, i, P) => { 
    const period_movements = { 
    base: C 
    } 
    period_movements.interest = C*i; 
    period_movements.amortization = P - (C*i); 
    period_movements.annuity = P; 
    period_movements.final_value = Math.round((C - period_movements.amortization)*100)/100; 

    return period_movements; 
} 

const display_mortgage = (C, i, n) => { 
    const payements = annuity(C, i, n); 
    let movements = balance_t(C, i, payements); 
    while (movements.final_value>-.01){ 
    console.log(movements); 
    movements = balance_t(movements.final_value, i, payements); 
    } 
} 

display_mortgage(20000, 0.05, 4); 

輸出:

{ base: 20000, 
    interest: 1000, 
    amortization: 4640.236652069255, 
    annuity: 5640.236652069255, 
    final_value: 15360 } 
{ base: 15360, 
    interest: 768, 
    amortization: 4872.236652069255, 
    annuity: 5640.236652069255, 
    final_value: 10488 } 
{ base: 10488, 
    interest: 524.4, 
    amortization: 5115.836652069255, 
    annuity: 5640.236652069255, 
    final_value: 5372 } 
{ base: 5372, 
    interest: 268.6, 
    amortization: 5371.6366520692545, 
    annuity: 5640.236652069255, 
    final_value: 0 } 
+1

感謝您的幫助! – Jol