Menu

Three number fields on our free tools, three different places they are clamped, and a comment promising sixteen cards from a sheet of four
📰

Three number fields on our free tools, three different places they are clamped, and a comment promising sixteen cards from a sheet of four

DEV Community: react·Daniel Pertu·15 days ago
#7mGTiiSZ
#devtoreact#devto#dev#code#rounds
Reading 0:00
15s threshold

PubTrivia's four free tools are the only pages on the site a stranger can type into without an account. There is no API behind them: a team name generator, a round planner, a scoresheet PDF builder and a QR code generator, all of them client-side, which means every limit they have is a limit somebody wrote in a React component.

I probed all three of the ones that take input, on the live pages, and found three different strategies. None of them is wrong. The interesting part is that nobody decided to have three.

One: clamp in the state setter

The scoresheet generator builds a printable answer sheet and score grid. Its inputs carry the bounds twice, once as HTML attributes and once in the code behind them:

const LIMITS = { rounds : { min : 1 , max : 12 }, questionsPerRound : { min : 1 , max : 20 }, teams : { min : 1 , max : 40 }, }

function clamp ( value : number , { min , max }: { min : number ; max : number }) { if ( ! Number . isFinite ( value )) return min return Math . max ( min , Math .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.