Menu

1 / 2

One scorer for seven question types: all-or-nothing, and the empty-key bug it is built to avoid

DEV Community: typescript·Emmanuel·2 days ago
#bTdlXMkU
#devtotypescript#devto#dev#question#type
Reading 0:00
15s threshold

Examenzaal is a practice-exam site for Dutch exams. The real exams mix question types: plain multiple choice, "tick all that apply", a grid where every row gets one column, a typed number, putting items in order, dragging items onto targets, and clicking a region on a picture. Our scoring code has to handle all of them, and it has to score the way the exam bodies do, where they publish how they score. Here is how that ended up as one small function, and the handful of rules that keep it honest.

One union for the answer key, one for the answer

Every question in an assembled exam carries an answer key, and the key is a tagged union with seven type tags:

export type AnswerKey = | { type : "single_choice" ; correctOptionId : string } | { type : "numeric" ; min : number ; max : number } | { type : "numeric" ; values : number [ ] } | { type : "sleep" ; placements : Placements } | { type : "hotspot" ; correctRegionId : string } | { type : "multi_select" ; correctOptionIds : string [ ] } | { type : "matrix" ;…

Continue reading — create a free account

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