Skip to content

Instantly share code, notes, and snippets.

@zeikeland
Last active April 29, 2017 15:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeikeland/e6d647645f3087dc6af730ef488bf363 to your computer and use it in GitHub Desktop.
Save zeikeland/e6d647645f3087dc6af730ef488bf363 to your computer and use it in GitHub Desktop.
Calculate mortage payments pr month
/**
* Function for calculating loan ammount.
* Sum is without fees and additional costs.
*/
function calculate_mortage( totalammount, ownmoney, interest, years ) {
/**
* Formula M = P * ( J / (1 - (1 + J)^-N))
* M = Payment monthly
* P = Loan ammount
* J = Effective intrest in decimal (x% / 100) / 12 months
* N = Num Payments
*
* Effective interest formula (1 + J)^12 – 1
*/
var m, p, j, n;
p = totalammount - ownmoney;
j = (interest / 100) / 12;
n = years * 12;
m = p * ( j / ( 1 - Math.pow(1+j, -n) ) );
// return with rounded monthly payment
return Math.round( m );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment