Loan Amortization
by Thet Hlaing
HTML
<div id="result">
</div>
JavaScript
function pmt(rate_per_period, number_of_payments, present_value, future_value=0, type=0){
if(rate_per_period != 0.0){
// Interest rate exists
var q = Math.pow(1 + rate_per_period, number_of_payments);
return -(rate_per_period * (future_value + (q * present_value))) / ((-1 + q) * (1 + rate_per_period * (type)));
} else if(number_of_payments != 0.0){
// No interest rate, but number of payments exists
return -(future_value + present_value) / number_of_payments;
}
return 0;
}
function loan_amortization(rate_per_period, period, loan_amount){
let result = [];
for(let i = 1; i <= period; i++){
let current_row = new LoanRepaymentRow();
current_row.period_number = i;
if(i === 1){
current_row.opening_balance = loan_amount;
}
else{
current_row.opening_balance = result[i-1].capital_outstanding;
}
current_row.loan_repayment = pmt(rate_per_period,period,-current_row.opening_balance).toFixed(2);
current_row.interest_charged = current_row.opening_balance*rate_per_period;
current_row.capital_repaid = current_row.loan_repayment - current_row.interest_charged;
current_row.capital_outstanding = current_row.opening_balance - current_row.capital_repaid;
result[i] = current_row;
}
console.dir(result);
return result;
}
class LoanRepaymentRow {
constructor(){
this.period_number = 0;
this.opening_balance = 0;
this.loan_repayment = 0;
this.interest_charged = 0;
this.capital_repaid = 0;
this.capital_outstanding = 0;
}
}
document.querySelector("#result").innerHTML=JSON.parse( loan_amortization(0.04,6,1500000));