Created
June 29, 2020 11:18
-
-
Save pejrak/99389bb5118399815c359b89c74b2b11 to your computer and use it in GitHub Desktop.
mortgage calculation script
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 loan = 100000 | |
const interestRate = 2.9 / 100 | |
const monthlyInterestRate = (interestRate / 12) | |
const numOfPayments = 360 | |
const interestRateExponent = Math.pow((monthlyInterestRate + 1), numOfPayments) | |
/** | |
* M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1] | |
* */ | |
const payment = loan * ( | |
monthlyInterestRate * interestRateExponent | |
) / ( | |
interestRateExponent - 1 | |
) | |
const totalToPay = (payment * numOfPayments) | |
const totalInterestToPay = (totalToPay - loan) | |
let totalPaid = 0 | |
let overPaid = 0 | |
const totalInterestIncrements = (numOfPayments * (1 + numOfPayments)) / 2 | |
const interestPerIncrement = totalInterestToPay / totalInterestIncrements | |
for (let paymentNum = 0; paymentNum < numOfPayments; paymentNum++) { | |
const remainingPayments = (numOfPayments - (paymentNum + 1)) | |
const interestInPayment = (remainingPayments * interestPerIncrement) | |
totalPaid += payment | |
overPaid += interestInPayment | |
console.log( | |
`${paymentNum},` + | |
`${payment.toFixed(2)},` + | |
`${interestInPayment.toFixed(2)},` + | |
`${overPaid.toFixed(2)}` | |
) | |
} | |
console.log({ | |
interestPerIncrement, | |
totalInterestIncrements, | |
totalInterestToPay, | |
totalPaid, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment