Quick answer: a loan EMI is P × r × (1 + r)^n ÷ ((1 + r)^n − 1), compound interest is P × (1 + r/n)^(n×t) and BMI is kg ÷ m². The functions below turn those formulas into code and handle the awkward cases. To check a result without code, use the EMI Calculator, the Compound Interest Calculator or the BMI Calculator.

The code

// Monthly loan instalment (EMI). principal: loan amount, annualRatePct: yearly rate in %, months: tenure in months function emi(principal, annualRatePct, months) { if (months <= 0) throw new Error("months must be positive"); const r = annualRatePct / 12 / 100; if (r === 0) return principal / months; // no interest const f = Math.pow(1 + r, months); return (principal * r * f) / (f - 1); } // Compound interest: final amount A = P (1 + r/n)^(n t) function compound(principal, annualRatePct, years, timesPerYear = 1) { const r = annualRatePct / 100; return principal * Math.pow(1 + r / timesPerYear, timesPerYear * years); } // BMI = kg / m^2, with the WHO bands function bmi(kg, meters) { const value = kg / (meters * meters); const band = value < 18.5 ? "underweight" : value < 25 ? "normal" : value < 30 ? "overweight" : "obese"; return { value: Math.round(value * 10) / 10, band }; } // Two discounts one after the other: 20% then 10% is 28% off, not 30% function stackedDiscountPct(...pcts) { const left = pcts.reduce((acc, p) => acc * (1 - p / 100), 1); return Math.round((1 - left) * 10000) / 100; } 

Results

Call Result
emi(1000000, 10, 120) about 13,215 per month
emi(1200, 0, 12) 100
compound(1000, 10, 2) 1,210
compound(1000, 12, 1, 12) 1,126.83
bmi(70, 1.75) 22.9, normal
bmi(95, 1.7) obese
stackedDiscountPct(20, 10) 28

These cases were run in Node before publishing.

Mistakes to avoid

  • Yearly rate used as monthly rate. In the EMI formula r is the monthly rate: yearly rate ÷ 12 ÷ 100. Using 10 instead of 0.00833 gives nonsense.
  • Zero interest. The standard EMI formula divides by (1 + r)^n − 1, which is zero when r is zero. The code handles that case separately.
  • Adding discounts. 20% off and then 10% off is 28%, not 30%, because the second discount applies to the reduced price.
  • Money in floating point. For real payments, store amounts in the smallest unit (cents, paisa) or use a decimal library, and round only when you show the number.
  • Height units for BMI. The formula needs metres. If a user types 175, convert from centimetres first.

BMI is only a screening number and not a diagnosis, so talk to a doctor about health decisions. Loan results are estimates: banks may add fees and use their own rounding.

More on the maths

The same formulas in plain words, with more examples, are in Percentage, EMI and BMI: simple formulas explained. For percentages and discounts there are the Percentage Calculator and the Discount Calculator.

Originally published on SaveAnyReel. I wrote this with AI assistance and ran the code in Node before publishing.