Live auto loan form computing monthly payment and total cost
A used-car dealership wants a payment estimator on every listing page. A shopper enters the car’s price, how much they put down, the loan’s APR, and how many months they want to pay. The page shows the monthly payment and what the loan costs in total, and it updates as they type.
The starter code includes the values the form starts with:
const DEFAULT_VALUES = { price: '28500', down: '3500', apr: '5.9', months: '60' };
Render four inputs, each with a visible <label> (or an aria-label), prefilled from DEFAULT_VALUES:
5.9.There is no Calculate button. The results update on every change to any input.
principal = price - down
r = apr / 100 / 12 (monthly rate)
n = months
monthly = principal * r / (1 - (1 + r) ^ -n) when r > 0
monthly = principal / n when r = 0
totalPaid = monthly * n
totalInterest = totalPaid - principal
Compute the totals from the unrounded monthly payment, and round only when you display a value.
When every input is valid, show these four lines. Each value is in dollars with exactly two decimals. A $ sign and thousands separators are optional ($25,000.00 and 25000.00 are both fine).
Loan amount: $25,000.00
Monthly payment: $...
Total interest: $...
Total paid: $...
Check the inputs in this order and show only the first message that applies. While a message is shown, don’t show any of the four result values.
Enter a valid price and down payment.Down payment must be less than the vehicle price.APR must be between 0 and 30.Term must be a whole number of months from 1 to 96.Once the input is fixed, the message disappears and the results come back.
price 10000, down 0, APR 12, 12 months
Loan amount: $10,000.00
Monthly payment: $888.49
Total interest: $661.85
Total paid: $10,661.85
price 12000, down 0, APR 0, 48 months
Monthly payment: $250.00
Total interest: $0.00
price 9000, down 9000 Down payment must be less than the vehicle price.
months 2.5 Term must be a whole number of months from 1 to 96.
principal / n instead.Number('') is 0, so check for empty text before converting.The tests find the inputs by their label (matching price, down payment, APR and term), type new values into them, and read the visible text. You don’t need test ids or class names. They check that:
Loan amount, Monthly payment, Total interest and Total paid, each followed by a number with two decimals. The monthly payment must match to the cent; the totals within 10 cents.Capitalization and extra spacing don’t matter. A colon after each result label is optional.