Skip to main content
πŸ’Ή Finance & Math Tools

What you're building

A compoundInterest(principal, rate, n, t) function that returns the final balance after compound interest is applied.

The formula:

A = P Γ— (1 + r/n)^(nΓ—t)
  • P β€” principal (starting amount)
  • r β€” annual interest rate as a decimal (e.g. 0.05 for 5 %)
  • n β€” compounding periods per year (12 = monthly, 4 = quarterly, 1 = annually)
  • t β€” time in years

Return the result rounded to 2 decimal places.

Requirements

  • Accept the four parameters above in the order (principal, rate, n, t)
  • Return a number (not a string) rounded to 2 decimal places
  • Handle edge cases: t = 0 returns principal; principal = 0 returns 0

Stretch goals

  • Add a compoundSchedule(principal, rate, n, t) that returns an array of yearly balances
  • Accept a monthly contribution parameter and add it each period

What this proves

You understand exponential growth, floating-point rounding, and function design β€” all common in fintech interviews.

⚑ 40 XP
solution.jsRuns in your browser
Loading editor…
Tests4 to pass
1000 at 5% annually for 10 years
1000 at 5% monthly for 1 year
t=0 returns principal unchanged
principal=0 returns 0

Run your code to check these.