DEV Community

Cover image for I Open-Sourced 15+ Core Financial & Math Algorithms in Vanilla JavaScript (Amortization, SIP, Compound Interest)
Raj
Raj

Posted on

I Open-Sourced 15+ Core Financial & Math Algorithms in Vanilla JavaScript (Amortization, SIP, Compound Interest)

Most calculator websites on the internet today suffer from two glaring problems:

  1. They are bloated with heavy third-party tracker scripts and sluggish layouts.
  2. The mathematical formulas powering them are buried in hidden server backends, making it impossible for developers or students to verify how the numbers were calculated.

To solve this, I built and open-sourced financial-math-formulas under the permissive MIT License.

Here is a breakdown of how the core financial algorithms work in clean, dependency-free Vanilla JavaScript.


1. Loan Amortization (Reducing Balance EMI)

Standard loan payments (car loans, personal loans, and mortgages) follow the international reducing-balance annuity formula:

EMI = P * [ r(1 + r)^n ] / [ (1 + r)^n - 1 ]

Where:

  • P = Principal loan balance
  • r = Monthly periodic interest rate (Annual APR / 12 / 100)
  • n = Total repayment months
/**
 * Calculates fixed monthly EMI for reducing-balance loans
 * @param {number} principal - Loan amount borrowed
 * @param {number} annualRatePercent - Annual interest rate (e.g. 7.5 for 7.5%)
 * @param {number} tenureYears - Loan duration in years
 * @returns {object} { monthlyEmi, totalPayment, totalInterest }
 */
function calculateLoanEmi(principal, annualRatePercent, tenureYears) {
  if (principal <= 0 || tenureYears <= 0) return { monthlyEmi: 0, totalPayment: 0, totalInterest: 0 };

  const n = tenureYears * 12;
  const r = (annualRatePercent / 100) / 12;

  if (r === 0) {
    const emi = principal / n;
    return { monthlyEmi: emi, totalPayment: principal, totalInterest: 0 };
  }

  const factor = Math.pow(1 + r, n);
  const monthlyEmi = principal * (r * factor) / (factor - 1);
  const totalPayment = monthlyEmi * n;
  const totalInterest = totalPayment - principal;

  return {
    monthlyEmi: Number(monthlyEmi.toFixed(2)),
    totalPayment: Number(totalPayment.toFixed(2)),
    totalInterest: Number(totalInterest.toFixed(2))
  };
}
Enter fullscreen mode Exit fullscreen mode

💡 Live Interactive Tool: You can test this algorithm with dynamic interactive amortization charts on the CalcCentrals EMI Calculator.


2. Systematic Investment Plan (SIP Annuity Due)

A Systematic Investment Plan (SIP) compounds fixed monthly deposits into mutual funds or index portfolios. Because deposits are made at the beginning of each monthly period, it follows the Annuity Due formula:

FV = P * [ ((1 + i)^n - 1) / i ] * (1 + i)

/**
 * Calculates future wealth growth from monthly SIP deposits
 * @param {number} monthlyDeposit - Fixed amount invested monthly
 * @param {number} annualReturnPercent - Expected annual growth rate (e.g. 12%)
 * @param {number} tenureYears - Investment horizon in years
 * @returns {object} { totalInvested, futureValue, wealthGain }
 */
function calculateSipFutureValue(monthlyDeposit, annualReturnPercent, tenureYears) {
  const n = tenureYears * 12;
  const i = (annualReturnPercent / 100) / 12;
  const totalInvested = monthlyDeposit * n;

  if (i === 0) {
    return { totalInvested, futureValue: totalInvested, wealthGain: 0 };
  }

  const futureValue = monthlyDeposit * ((Math.pow(1 + i, n) - 1) / i) * (1 + i);
  const wealthGain = futureValue - totalInvested;

  return {
    totalInvested: Number(totalInvested.toFixed(2)),
    futureValue: Number(futureValue.toFixed(2)),
    wealthGain: Number(wealthGain.toFixed(2))
  };
}
Enter fullscreen mode Exit fullscreen mode

💡 Live Interactive Tool: Compare multi-year compounding curves on the CalcCentrals SIP Calculator.


3. High-Precision Compound Interest

Compound interest differs by compounding frequency (n = 1 for annual, n = 12 for monthly, n = 365 for daily):

A = P * (1 + r / n)^(n * t)

/**
 * Calculates compound interest growth across customizable frequencies
 */
function calculateCompoundInterest(principal, annualRatePercent, years, timesCompoundedPerYear = 12) {
  const r = annualRatePercent / 100;
  const n = timesCompoundedPerYear;
  const t = years;

  const amount = principal * Math.pow(1 + (r / n), n * t);
  const totalInterest = amount - principal;

  return {
    futureBalance: Number(amount.toFixed(2)),
    totalInterest: Number(totalInterest.toFixed(2))
  };
}
Enter fullscreen mode Exit fullscreen mode

💡 Live Interactive Tool: Test daily vs. monthly compounding on the CalcCentrals Compound Interest Calculator.


4. Full PITI Mortgage Calculation

A real-world mortgage involves more than just principal and interest; it must factor in PITI (Principal, Interest, Property Taxes, Homeowners Insurance, and PMI):

function calculateMortgagePiti(homePrice, downPaymentPercent, annualRate, years, annualTax, annualInsurance, pmiPercent = 0) {
  const downPayment = homePrice * (downPaymentPercent / 100);
  const principal = homePrice - downPayment;

  const n = years * 12;
  const r = (annualRate / 100) / 12;
  const monthlyPi = principal * (r * Math.pow(1 + r, n)) / (Math.pow(1 + r, n) - 1);

  const monthlyTax = annualTax / 12;
  const monthlyInsurance = annualInsurance / 12;
  const monthlyPmi = (downPaymentPercent < 20) ? (principal * (pmiPercent / 100)) / 12 : 0;

  const totalMonthlyPiti = monthlyPi + monthlyTax + monthlyInsurance + monthlyPmi;

  return {
    monthlyPi: Number(monthlyPi.toFixed(2)),
    totalMonthlyPiti: Number(totalMonthlyPiti.toFixed(2)),
    monthlyPmi: Number(monthlyPmi.toFixed(2))
  };
}
Enter fullscreen mode Exit fullscreen mode

💡 Live Interactive Tool: Test loan scenarios with amortization schedules on the CalcCentrals Mortgage Calculator.


Open Source Repository & Contributions

All formulas are completely dependency-free, zero-framework, and published under the MIT license.

Feel free to star the repo, fork it for your own web apps, or submit pull requests with additional mathematical models!

Top comments (0)