Skip to content

Instantly share code, notes, and snippets.

@daigofuji
Created March 30, 2023 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daigofuji/7bc42b95c4d83b86de5562188c484fdf to your computer and use it in GitHub Desktop.
Save daigofuji/7bc42b95c4d83b86de5562188c484fdf to your computer and use it in GitHub Desktop.
amortization formula in JavaScript
// P: principal (loan amount)
// r: monthly interest rate
// M: monthly payment
// n: number of months
const P = 10000; // example loan amount
const r = 0.015; // example monthly interest rate (18% APR / 12 months)
const M = 100; // example monthly payment amount
let n = Math.log(M / (M - r * P)) / Math.log(1 + r); // calculate number of months
n = Math.ceil(n); // round up to the nearest whole number
console.log(n); // output: the number of months to pay off the loan with fixed monthly payments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment