Dev.to WebDev 🛠 Dev 👁 0 📖 4 min read

How we built 35 construction calculators with plain JavaScript and one shared formulas file

Construction math looks simple until you have to get it right for a stranger. "How many 80 lb bags of concrete for this slab?" mixes feet, inches, cubic yards, a waste allowance, and a bag yield that is a property of the

Construction math looks simple until you have to get it right for a stranger. "How many 80 lb bags of concrete for this slab?" mixes feet, inches, cubic yards, a waste allowance, and a bag yield that is a property of the product rather than the geometry. Get one unit wrong and someone buys twice the concrete they need.

We run Free Construction Calculators, a set of 35 free calculators for concrete, gravel, mulch, roofing, paint, drywall, stairs and more. This post is about the structure that kept those 35 pages consistent: no framework, no build step in the browser, and one file of pure functions that every calculator shares.

The rule: all internal math in feet and cubic feet

Every input gets converted to feet (or inches for small depths) at the edge, and every function inside works in one unit system. Metric users still type metres and centimetres. The UI layer converts on the way in and on the way out.

var FT3_PER_YD3 = 27;
var M3_PER_FT3 = 0.0283168466;

// Pre-mixed concrete bag yields in cubic feet.
var BAG_YIELD_FT3 = { 40: 0.30, 60: 0.45, 80: 0.60 };

function pos(n) { n = Number(n); return isFinite(n) && n > 0 ? n : 0; }

pos() is three short lines that do a lot of work. Empty fields, negative numbers, NaN from a half-typed decimal: all become 0, so a calculator never shows NaN cubic yards mid-typing.

Small pure functions, composed per calculator

The shared file exposes functions like these:

slabFt3: function (lengthFt, widthFt, thicknessIn) {
  return pos(lengthFt) * pos(widthFt) * pos(thicknessIn) / 12;
},
cylinderFt3: function (diameterIn, heightFt) {
  var r = pos(diameterIn) / 24; // inches -> feet radius
  return Math.PI * r * r * pos(heightFt);
},
withWaste: function (qty, wastePct) {
  return pos(qty) * (1 + pos(wastePct) / 100);
},
bagsNeeded: function (ft3, bagYieldFt3) {
  if (!pos(ft3) || !pos(bagYieldFt3)) return 0;
  return Math.ceil(ft3 / bagYieldFt3 - 1e-9);
},

The - 1e-9 in bagsNeeded matters. Floating point can turn an exact 10 bags into 10.000000000000002, and Math.ceil would round that up to 11. Subtracting a tiny epsilon before rounding up fixes it without hiding real fractions.

Each calculator page then gets a tiny script of its own. Here is the asphalt one, slightly trimmed:

(function () {
  var F = window.FCC, UI = window.FCCUI;
  UI.init("asphalt-form", function (form) {
    var shape = form.elements.shape.value;
    var areaFt2 = shape === "circle" ? F.circleAreaFt2(UI.lenFt(form, "cD"))
      : shape === "tri" ? F.triangleAreaFt2(UI.lenFt(form, "tB"), UI.lenFt(form, "tH"))
      : UI.lenFt(form, "rL") * UI.lenFt(form, "rW");
    var ft3 = F.withWaste(F.areaVolumeFt3(areaFt2, UI.smallIn(form, "depth")), UI.num(form, "waste"));
    var tons = ft3 * UI.num(form, "density") / 2000;
    UI.set("r-tons", UI.fmt(tons));
    UI.set("r-yd3", UI.fmt(F.ft3ToYd3(ft3)));
  });
})();

UI.init wires the form so results update on every input event, and UI.lenFt reads a length field in whatever unit the user picked and returns feet. The page-level script is only the recipe: shape → area → volume → waste → output units.

Same file in the browser and in the build

The formulas file is wrapped so it works as a browser global and as a Node module:

(function (root) {
  var F = { /* ...functions... */ };
  if (typeof module !== "undefined" && module.exports) module.exports = F;
  else root.FCC = F;
})(this);

The static-site build imports that exact file and uses it to compute every worked example and reference table on the pages. The "10 × 10 ft slab at 4 in" row on the concrete page is not typed by hand. It comes from F.slabFt3(10, 10, 4) and F.concreteBags(v, 80), the same calls the live calculator makes. If a formula changes, every example table changes with it, so the text and the calculator can't drift apart.

Static pages, generated once

The pages themselves are plain HTML generated by a small Node build script from a list of calculators: title, inputs, result fields, formula text and a worked example. The output is a folder of static files pushed to GitHub, which deploys to ordinary shared hosting. There is no server code for the calculators, so pages load fast on a phone at a job site.

Because every page is generated from the same template, adding a feature once adds it everywhere. Two examples:

  • Formula and worked example on every page. Users can check the math instead of trusting a black box.
  • Embeddable widgets. Six calculators have an /embed/ version that suppliers and contractors can drop into their own site with one <iframe>.

Practices that helped

  • Fix rounding rules up front. Bags round up, cubic yards show two decimals, and costs show whole dollars. Writing these down once keeps 35 pages consistent.
  • Keep material constants in one place. Bag yields, densities and coverage figures live in the shared file, so two pages can never disagree about what an 80 lb bag yields.
  • Write the worked example before the calculator. If the example can't be done by hand, the calculator isn't ready.

If you want to see how it turned out, the concrete calculator shows cubic yards and 40/60/80 lb bag counts side by side, and every page lists its formula. Questions about the approach are welcome in the comments.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.