Pre-built code blocks

Mortgage calculator and weekly rates

Mortgage math and weekly Freddie Mac rates: payment, full monthly cost, affordability

For when the answer to “It has a mortgage payment or affordability calculator” is yes.

Gives a real estate or finance site a correct mortgage payment calculator, a how-much-house-can-I-afford solver and this week's national average rates from Freddie Mac, in two small files with no dependencies.

Skip the rebuild

You do not write this one. It arrives in your project as ordinary source you can read, change and keep, with its audit record and its caveats beside it.

  1. Download the Playground. It is free and runs on your own machine.
  2. Start a new app and tick “It has a mortgage payment or affordability calculator”.
  3. This block is written in, along with anything else you ticked.

Download the Playground See the other blocks

The mistake it removes

Hand-rolled mortgage math divides by zero at a 0% rate, forgets PMI below 20% down, or solves affordability for a price whose real payment overshoots the budget. A rates widget that fetches on every page view goes blank the moment the source is down.

What is already handled

Each of these was checked by running the code, not by reading it.

What you still have to do

A copied file cannot own your secrets, your host or your legal obligations. This is the part the block deliberately does not claim.

What lands in your project

vanilla

  • vanilla/mortgage-math.js

payment(principal, ratePct, years), monthlyCost({price, down, rate, ...}), affordPrice({income, debts, down, rate, ...}). Pure functions: runs in the browser and in Node.

node

  • node/pmms-rates.js

latestRates() -> { week, rate30, rate15, prev30, prev15, yearAgo30, source, sourceUrl } or null. createRates({ file, userAgent, fetch }) for your own settings. Serve it from an API route; do not call Freddie Mac from the browser.

What you supply

Licensed MIT. It is a starting point, not a finished product.

Get it

Download the Playground See the other blocks

Nothing here is locked. The files are yours, in your folder, under a permissive license.

Questions

Is Mortgage calculator and weekly rates audited?
No. No full audit has been run against this block yet, which is not the same as a pass. What is verified about it is listed on this page, and what is not is listed beside it.
What do I still have to do myself?
Show the source and sourceUrl that latestRates() returns next to every rate you display, and say it is a national weekly average and not a quote.
How do I get this code?
Download the Playground, start a new app, and tick “It has a mortgage payment or affordability calculator”. The block is written into your project as ordinary source you can read and edit.

All pre-built code blocks · Learn to build from zero · The coding guide