Stay up to date with AI tools, model releases, and developer workflows that matter.

Weekly. Free. One click to leave.

7 Day Free Trial. Cancel Anytime.

Ask anyone selling an old chain or a broken ring what they want to know, and it's one thing: what is this worth? A small calculator can answer that in a second, but most of the ones you find online get the details wrong. They ignore troy ounces, treat 14K as 58.5% one day and 58.3% the next, or show a "value" that no buyer will ever pay.

According to recent reports, in this tutorial, we'll build a gold value calculator that gets those details right. It runs on plain HTML and JavaScript, updates as the user types, works with a screen reader, keeps its state in a shareable URL and pulls a live spot price through a small Node.js endpoint. The calculation lives in one pure function, so we can test it in a few lines.

Every gold calculator comes down to three inputs: how much the item weighs, how much of that weight is actually gold, and what pure gold is trading for today.

Purity is measured in karats, out of 24. A 14K ring is 14 parts gold and 10 parts other metals, so it's 14 รท 24 = 58.3% gold. You'll also see 14K stamped as "585", which is the hallmark many manufacturers use for the same alloy. For a calculator, the karat fraction is the safer default:.

According to recent reports, the second detail most tutorials miss is the unit. Gold spot prices are quoted in USD per troy ounce, which is 31.1035 grams, not the 28.35-gram ounce on a kitchen scale. Jewelers also use pennyweight (dwt), which is 1.5552 grams. If your calculator multiplies grams by a per-ounce price without converting, every result is wrong by a factor of 31.

Put together, the formula looks like this:.

For example, with gold at $2,400 per troy ounce, 10 grams of 14K gold contains 5.83 grams of pure gold, which is worth $449.85 at melt.

That $449.85 is the value of the gold content alone. Refiners and buyers pay a percentage of it, because refining, assaying and handling cost money. Depending on the buyer, the item and the market, the payout can sit well below melt value.

That gap is why good production tools separate the two numbers. A dedicated scrap jewelry gold calculator, like the one The Alloy Market offers, lets sellers estimate what their gold is worth from its weight and karat before they send anything in. Our calculator does the same job with a payout field: leave it at 100% to see melt value, or enter a buyer's rate to see a realistic offer.

Start with real form controls and a visible for each one. Placeholder text is not a label: it disappears as soon as the user types, and screen readers handle it inconsistently. The W3C forms tutorial covers the rules in detail.

A few choices here matter more than they look:.

Recent findings suggest that A little CSS gives touch-friendly controls. Keeping inputs at 16px (1rem) also stops iOS Safari from zooming in when a field gets focus.

It has been revealed that the biggest problem with most calculator snippets is that the math is buried inside an event handler, mixed with getElementById calls. You can't test it, and you can't reuse it on a server. Put the calculation and validation in their own module instead:.

Both functions take plain values and return plain values. They don't know a DOM exists, which means the same file can run in the browser, in Node.js and in your test runner.

Industry observers note that notice that we don't round inside the calculation. Rounding happens once, at display time. Rounding in the middle of a chain of multiplications is a classic way to end up a few cents off.

Industry observers note that now connect the form to the module. Listening for the input event on the form itself catches changes from every field, including the dropdowns, so there's no need to wire up each control separately.

With the markup, CSS and script in place, here's the finished calculator showing our 10 g, 14K example with an 80% buyer payout:.

Here's what this code is doing:.

It has been revealed that asking people to type the spot price works, but prefilling it is friendlier. Don't call a metals price API straight from the browser, though. Your API key would be visible to anyone who opens DevTools, and every visitor would spend one of your API calls.

Recent findings suggest that instead, put a small endpoint in front of your price provider and cache the result. This example uses Express and the fetch built into Node.js 18 and later:.

Three things keep this endpoint dependable:.

It has been revealed that on the page, the loadSpot() function from Step 3 fills the field and shows the time of the price. The field stays editable, so a seller who has a quote from a specific buyer can type that instead.

It has been revealed that A seller comparing offers might want to send their numbers to a family member, or come back to them tomorrow. Because every input has a name, turning the form into a query string is one line with URLSearchParams.

Industry observers note that the syncUrl() function in Step 3 runs on every change and uses history.replaceState instead of pushState, so typing doesn't fill the back button with dozens of entries. On load, restoreFromUrl() puts the values back. A link like this opens with the form already filled in:.

Industry observers note that this is also the progressive enhancement story. If JavaScript fails to load, the form still submits as a GET request. Your server can read the same parameters, import the same gold.js and render the result in HTML.

It has been revealed that because goldValue() is a pure function, testing it needs no browser and no mocks. Node.js ships with a built-in test runner, so there's nothing to install:.

Run it with node --test. (Add "type": "module" to your package.json so Node treats the files as ES modules.) The troy ounce test is the one worth keeping forever: if anyone "simplifies" the conversion later, it fails immediately.

A quick checklist for putting this calculator on a real store:.

A gold calculator looks like a ten-line script, but the details are what make it useful: converting troy ounces properly, separating melt value from payout, keeping the math testable and making the result accessible and shareable. The same pattern of a pure calculation module, a thin DOM layer and a cached server endpoint works for any calculator where the price depends on several inputs, from shipping estimates to currency conversion.

Recent findings suggest that mathematically, 14K is 14 รท 24 = 58.3%. Many manufacturers stamp 14K jewelry "585" and alloy it to that standard, so both numbers are common. Pick one, state it in the dropdown, and use it consistently.

In a recent development, let the user choose. Most people weigh jewelry in grams, but spot prices are quoted per troy ounce, so the calculator should accept either and convert internally.

Yes. Drop Step 4 and let customers type the spot price, or have a scheduled job write the current price into a static JSON file. Everything else runs entirely in the browser.

It has been revealed that 7 Day Free Trial. Cancel Anytime.

โ†’ Read original article