What you're building
A amortize(principal, annualRate, months) function that returns an array of payment objects, one per month, modelling a fully amortising fixed-rate loan.
Each object in the array:
{
month: 1, // payment number (1-indexed)
payment: 537.30, // fixed monthly payment (same every month)
principal: 412.30, // portion paying down the balance
interest: 125.00, // portion going to interest
balance: 99587.70 // remaining balance after this payment
}
All monetary values rounded to 2 decimal places.
Monthly payment formula
monthlyRate = annualRate / 12
payment = P Γ monthlyRate / (1 β (1 + monthlyRate)^(βmonths))
When annualRate === 0, the payment is simply principal / months.
Requirements
- Return an array of exactly
monthsobjects with the shape above - The final
balancemust be0(adjust the last payment for rounding drift) - Interest for each period =
balance_before Γ monthlyRate, rounded to 2 dp - Principal for each period =
payment β interest
Stretch goals
- Accept an extra-payment parameter to pay down faster
- Add a summary object:
{ totalPaid, totalInterest }
What this proves
You can implement real financial formulas, handle floating-point precision carefully, and return structured data β all valuable in any data-heavy role.
β‘ 50 XP
solution.jsRuns in your browser
Loading editorβ¦
Tests4 to pass
returns correct number of payments
first month interest is correct
final balance is 0
zero interest splits evenly
Run your code to check these.