Created
August 20, 2019 07:34
-
-
Save Apmyp/c7b932f129c906bfe9f2be5e93ad0d9f to your computer and use it in GitHub Desktop.
Annuity and linear payments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const principal = 100000; | |
const interestRate = 0.12 / 12; | |
const instalments = 24; | |
function annuityFactor(interestRate, instalments) { | |
return (interestRate + ( interestRate / ( Math.pow(1 + interestRate, instalments) - 1 ) ) ); | |
} | |
function annuityPayment(principal, interestRate, instalments) { | |
return principal * annuityFactor(interestRate, instalments); | |
} | |
function annuityTotalCost(principal, interestRate, instalments, annuityPayment) { | |
return annuityPayment * instalments; | |
} | |
function annuityOverpayment(principal, interestRate, instalments, annuityPayment) { | |
return annuityTotalCost(principal, interestRate, instalments, annuityPayment) - principal; | |
} | |
function percentageInAnnuityPayment(annuityPayment, balance, interestRate, period) { | |
if( period < 1) { | |
return 0; | |
} else if(period === 1) { | |
return balance * interestRate; | |
} else if (period > 1) { | |
const currentPaymentBody = annuityPayment - (balance * interestRate); | |
const remainBalance = balance - currentPaymentBody; | |
return percentageInAnnuityPayment(annuityPayment, remainBalance, interestRate, period - 1); | |
} | |
} | |
function bodyInAnnuityPayment(annuityPayment, balance, interestRate, period) { | |
return annuityPayment - percentageInAnnuityPayment(annuityPayment, balance, interestRate, period); | |
} | |
function percentageInLinearPayment(balance, interestRate, body, period) { | |
return (balance - ((period-1) * body)) * interestRate; | |
} | |
function bodyInLinearPayment(balance, instalments) { | |
return balance / instalments; | |
} | |
function linearPayment(principal, interestRate, body, period) { | |
const percentage = percentageInLinearPayment(principal, interestRate, body, period); | |
return percentage + body; | |
} | |
function linearTotalCost(principal, interestRate, instalments, body) { | |
function calculateLinearPayment(_, index) { | |
return linearPayment(principal, interestRate, body, index + 1); | |
} | |
function sumPayments(acc, item) { | |
return acc + item; | |
} | |
return (Array.apply(null, Array(instalments))) | |
.map(calculateLinearPayment) | |
.reduce(sumPayments); | |
} | |
function linearOverpayment(principal, interestRate, instalments, body) { | |
return linearTotalCost(principal, interestRate, instalments, body) - principal; | |
} | |
function annualPercentageRateOfCharge(totalCost, principal, instalments) { | |
return (( (totalCost / principal) - 1 ) / (instalments / 12)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment