Mortgage Calculator
UI Coding
Easy
20 min
forms
derived-state
i18n
accessibility
Build a mortgage calculator for a fixed-rate loan with monthly payments.
Inputs: Loan amount ($), Annual interest rate (%), Loan term (years).
Formula: with principal P, monthly rate r = annualRate / 100 / 12 and n = years × 12 payments:
M = P × r × (1 + r)^n / ((1 + r)^n − 1)
Requirements
- Results update as the user types. There's no Calculate button.
- Show:
- Monthly payment
- Total paid (
M × n) - Total interest (
total − P)
- Format money as US dollars with two decimals and thousands separators (
$1,234.56). - A 0% interest rate is valid. The payment is then simply
P / n. - Invalid input (empty, negative, a loan amount of 0, a term that isn't a whole number from 1 to 50): mark the field
aria-invalid, explain the problem, and show "—" for the results instead ofNaNorInfinity. - Defaults: $300,000, 6.5%, 30 years.
- Results are announced politely to screen readers when they change.
Follow-up
- Why compute the results during render instead of storing them in state?
- Floating-point and money: what can go wrong, and what would a bank do?
- How would you add an amortisation schedule for 360 rows efficiently?
Spotted a mistake or a rights concern? Report an issue