{"slug": "build-a-score-lookup-calculator-with-data-tables-and-interpolation", "title": "Build a Score Lookup Calculator with Data Tables and Interpolation", "summary": "A developer outlined a table-driven JavaScript pattern for score lookup calculators, keeping scoring tables as sorted data arrays and using a single lookup function with a direction flag to handle both \"higher is better\" and \"lower is better\" metrics. The approach, drawn from work on the aftptcalc.com fitness score tool, adds optional linear interpolation for stepped tables, input parsing and validation, and edge-case tests generated from the table rows. The developer recommends versioning and dating each dataset so revised tables can be tracked.", "body_md": "*Disclosure: I used an AI assistant to help draft and edit this article. The code is illustrative and I ran it by hand.*\n\nMany calculators boil down to the same job: take a raw measurement, look it up in a published table, and return points. Fitness tests, grade conversions, shipping rates, and tax brackets all work this way. If you hard-code the logic as a wall of `if` statements, the first time the table changes you will be rewriting code. A better approach is to keep the table as data and write one small function that reads it. This post shows a pattern in plain JavaScript that handles both \"higher is better\" and \"lower is better\" scores.\n\nI picked up the approach while working on a score lookup tool, [aftptcalc.com](https://aftptcalc.com/), where each event has its own table and the tables change when the rules change. The same idea works for any table-driven calculator.\n\nWrite each event as an array of rows, sorted from best to worst. Each row is a threshold and the points it earns.\n\n``` js\nconst pushups = [\n  { reps: 60, points: 100 },\n  { reps: 50, points: 90 },\n  { reps: 40, points: 80 },\n  { reps: 30, points: 70 },\n  { reps: 20, points: 60 },\n];\n\nconst run = [\n  { seconds: 780, points: 100 },\n  { seconds: 840, points: 90 },\n  { seconds: 900, points: 80 },\n  { seconds: 960, points: 70 },\n  { seconds: 1020, points: 60 },\n];\n```\n\nThese numbers are placeholders for the tutorial, not real standards. When you build the real thing, copy values from the publisher's own table and note the source and date next to the data.\n\nThe only difference between a rep count and a run time is direction. Pass a flag instead of writing two functions.\n\n``` js\nfunction lookup(table, key, value, higherIsBetter) {\n  for (const row of table) {\n    const meets = higherIsBetter ? value >= row[key] : value <= row[key];\n    if (meets) return row.points;\n  }\n  return 0; // below the lowest row\n}\n\nlookup(pushups, \"reps\", 45, true);   // 80\nlookup(run, \"seconds\", 870, false);  // 80\n```\n\nBecause the rows are sorted best to worst, the first row the value meets is the answer. That is easy to reason about and easy to test.\n\nSome published tables list every possible value, so exact lookup is all you need. Others list only certain steps, and you must decide what happens between them. Check the source before you decide. If the publisher awards points only at listed values, do not interpolate, because you would be inventing scores. If you are building a training estimate rather than a scorer, linear interpolation can be a useful approximation, as long as you label it.\n\n``` js\nfunction interpolate(a, b, value, key) {\n  const t = (value - a[key]) / (b[key] - a[key]);\n  return Math.round(a.points + t * (b.points - a.points));\n}\n```\n\nMost calculator bugs come from inputs, not math. Check three things: the value is a number, it is within a plausible range, and it is in the right unit. A run time typed as `15:30` needs parsing into seconds first.\n\n``` js\nfunction parseTime(text) {\n  const m = /^(\\d{1,2}):([0-5]\\d)$/.exec(text.trim());\n  if (!m) return null;\n  return Number(m[1]) * 60 + Number(m[2]);\n}\n```\n\nReturn `null` for bad input and show a clear message, rather than silently returning zero. A zero looks like a real result and confuses people.\n\nTable lookups fail at the edges, so test there. For each row, test the exact threshold, one unit better, and one unit worse. A small loop can generate these tests automatically from the table itself.\n\n``` js\nfor (const row of pushups) {\n  console.assert(lookup(pushups, \"reps\", row.reps, true) >= row.points);\n  console.assert(lookup(pushups, \"reps\", row.reps - 1, true) <= row.points);\n}\n```\n\nAlso test the extremes: zero, a negative number, and a value far better than the best row. Decide ahead of time whether a value above the top row should cap at the maximum, and write that into a test so nobody changes it by accident.\n\nTables get revised. Put a `version` and `effectiveDate` field next to each dataset and show them in the interface. When someone asks why their score looks different from last year, you can point to the table version instead of guessing. Keeping data in its own file also means a non-programmer can review a change in a pull request without reading any logic.\n\n**Mixing units.** Seconds in the table and minutes in the input is the classic one. Normalize everything on the way in.\n\n**Floating-point surprises.** Keep run times as integer seconds and body measurements as the unit the source uses, so you are not comparing 14.999999 with 15.\n\n**Ties and ordering.** If two rows share a threshold, the first one wins. Sort and de-duplicate when you load the data.\n\n**Silent fallbacks.** A default of zero or a hidden cap hides mistakes. Make fallbacks visible in the result.\n\nKeep the data in tables, keep the lookup function tiny, validate inputs, and test the boundaries. With that structure, updating a calculator when the rules change becomes a data edit rather than a rewrite. If you handle table lookups differently, for example with binary search for very large tables, I would be glad to hear about it in the comments.", "url": "https://wpnews.pro/news/build-a-score-lookup-calculator-with-data-tables-and-interpolation", "canonical_source": "https://dev.to/leenecasella/build-a-score-lookup-calculator-with-data-tables-and-interpolation-4k03", "published_at": "2026-12-06 22:06:46+00:00", "updated_at": "2026-12-06 22:14:28.263727+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["aftptcalc.com"], "also_reported_by": [], "takeaway": "Store scoring tables as sorted data arrays and use one lookup function with a higherIsBetter flag, returning null rather than 0 for bad input so invalid entries are not mistaken for real scores.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/build-a-score-lookup-calculator-with-data-tables-and-interpolation", "markdown": "https://wpnews.pro/news/build-a-score-lookup-calculator-with-data-tables-and-interpolation.md", "text": "https://wpnews.pro/news/build-a-score-lookup-calculator-with-data-tables-and-interpolation.txt", "jsonld": "https://wpnews.pro/news/build-a-score-lookup-calculator-with-data-tables-and-interpolation.jsonld"}}