Build a Score Lookup Calculator with Data Tables and Interpolation 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. Disclosure: I used an AI assistant to help draft and edit this article. The code is illustrative and I ran it by hand. Many 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. I 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. Write each event as an array of rows, sorted from best to worst. Each row is a threshold and the points it earns. js const pushups = { reps: 60, points: 100 }, { reps: 50, points: 90 }, { reps: 40, points: 80 }, { reps: 30, points: 70 }, { reps: 20, points: 60 }, ; const run = { seconds: 780, points: 100 }, { seconds: 840, points: 90 }, { seconds: 900, points: 80 }, { seconds: 960, points: 70 }, { seconds: 1020, points: 60 }, ; These 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. The only difference between a rep count and a run time is direction. Pass a flag instead of writing two functions. js function lookup table, key, value, higherIsBetter { for const row of table { const meets = higherIsBetter ? value = row key : value <= row key ; if meets return row.points; } return 0; // below the lowest row } lookup pushups, "reps", 45, true ; // 80 lookup run, "seconds", 870, false ; // 80 Because 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. Some 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. js function interpolate a, b, value, key { const t = value - a key / b key - a key ; return Math.round a.points + t b.points - a.points ; } Most 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. js function parseTime text { const m = /^ \d{1,2} : 0-5 \d $/.exec text.trim ; if m return null; return Number m 1 60 + Number m 2 ; } Return null for bad input and show a clear message, rather than silently returning zero. A zero looks like a real result and confuses people. Table 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. js for const row of pushups { console.assert lookup pushups, "reps", row.reps, true = row.points ; console.assert lookup pushups, "reps", row.reps - 1, true <= row.points ; } Also 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. Tables 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. Mixing units. Seconds in the table and minutes in the input is the classic one. Normalize everything on the way in. 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. Ties and ordering. If two rows share a threshold, the first one wins. Sort and de-duplicate when you load the data. Silent fallbacks. A default of zero or a hidden cap hides mistakes. Make fallbacks visible in the result. Keep 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.