Skip to main content

Spaced Repetition for Exam Prep Apps: Implementing SM-2 in JavaScript

A compact SuperMemo 2 implementation, a sensible mapping from multiple-choice answers to quality scores, and how to apply it to a CNA question bank

Updated 7 min readMarkdown

By Winfred, CNA TestBank

Cramming a question bank the night before a test feels productive and fades quickly. Spaced repetition is the usual fix: show an item again just as it is getting hard to recall, and stretch the interval as it gets easier. The SuperMemo 2 algorithm, published by Piotr Wozniak in the late 1980s and widely documented, is still a good default because it is small, testable, and easy to explain to users.

This article implements SM-2 in JavaScript, then maps it onto a multiple-choice healthcare practice bank — the same kind of product as the free CNA practice questions on CNA TestBank. It does not claim that any particular site already uses this scheduler. This is independent exam-prep education, not official examination content, and it is not affiliated with any testing vendor.

The SM-2 state you have to store

Each item, for each learner, carries three numbers:

  • repetition (n): how many times the learner has answered it successfully in a row. A failure resets this to 0.
  • interval: the number of days until the item is due again.
  • easiness (EF): a factor that starts at 2.5 and drifts as the learner does well or poorly, with a floor of 1.3.

You also store dueAt, a timestamp you compute from the interval after each review. A session is then "all items that are due, plus a small number of new items," not "the entire bank in a random order."

The quality score q is an integer from 0 to 5. In the classic algorithm, a score of 3 or higher counts as a success; 0, 1, or 2 is a failure and restarts the repetition count.

A straightforward implementation

const MIN_EF = 1.3;
const DEFAULT_EF = 2.5;

export function reviewCard(card, quality) {
  if (quality < 0 || quality > 5 || !Number.isInteger(quality)) {
    throw new Error("quality must be an integer from 0 to 5");
  }

  let { repetition, interval, easiness } = card;
  easiness = easiness ?? DEFAULT_EF;
  repetition = repetition ?? 0;
  interval = interval ?? 0;

  const delta = 0.1 - (5 - quality) * (0.08 + (5 - quality) * 0.02);
  easiness = Math.max(MIN_EF, easiness + delta);

  if (quality < 3) {
    repetition = 0;
    interval = 1;
  } else {
    if (repetition === 0) interval = 1;
    else if (repetition === 1) interval = 6;
    else interval = Math.round(interval * easiness);
    repetition += 1;
  }

  const dueAt = new Date();
  dueAt.setDate(dueAt.getDate() + interval);

  return { ...card, repetition, interval, easiness, quality, dueAt };
}

export function isDue(card, now = new Date()) {
  if (!card.dueAt) return true;
  return new Date(card.dueAt) <= now;
}

That is the whole scheduler. The first successful review comes back in one day, the second in six days, and later reviews are round(interval * easiness) days later. A miss does not throw the item out of the bank; it puts it back on tomorrow's list and lets the easiness factor drop so future intervals grow more slowly.

A tiny set of tests will save you from off-by-one bugs in the first two repetitions:

const fresh = { repetition: 0, interval: 0, easiness: 2.5 };

const pass1 = reviewCard(fresh, 5);
// pass1.interval === 1, pass1.repetition === 1

const pass2 = reviewCard(pass1, 4);
// pass2.interval === 6, pass2.repetition === 2

const pass3 = reviewCard(pass2, 5);
// pass3.interval === Math.round(6 * pass3.easiness) after the EF update

const miss = reviewCard(pass3, 1);
// miss.repetition === 0, miss.interval === 1

Keep the function pure. Persist the returned object. If you mutate the card in place and also write an optimistic UI, you will lose a review when a save fails.

Mapping a multiple-choice attempt to a quality score

SM-2 was designed for recall cards ("What is the capital of France?"). A four-option CNA item is a recognition task, and a lucky guess looks like mastery if you treat every green check as a 5. You need a mapping that is honest about that.

A practical scale for a practice-test UI:

  • 0 — skipped, or timed out with no answer.
  • 1 — incorrect, and the learner flagged it as a guess or answered very quickly.
  • 2 — incorrect after some thought.
  • 3 — correct, but slow, or the learner changed the answer before submitting.
  • 4 — correct at a normal pace, no change of mind.
  • 5 — correct and fast, or the learner later self-rates "I knew this cold."

You can implement the middle of that scale without a self-rating widget. Capture elapsed time, whether the selected radio changed, and whether the answer was right. Then:

function qualityFromAttempt({ correct, elapsedMs, changedAnswer, skipped }) {
  if (skipped) return 0;
  if (!correct) return elapsedMs < 8000 ? 1 : 2;
  if (changedAnswer || elapsedMs > 45000) return 3;
  if (elapsedMs < 12000) return 5;
  return 4;
}

The time thresholds are product choices, not laws. Clinical stems on a site like CNA TestBank are longer than flashcard prompts, so a twelve-second "fast" cutoff that works for a vocabulary deck will punish careful reading. Raise the thresholds for long stems, or drop time from the mapping and ask one question after the rationale: "Could you have explained this without the options?" Yes becomes a 5, "I was pretty sure" a 4, and "I guessed" a 3 even when the answer was correct. An honest 3 is more useful than a vanity 5.

Never award a 3 or higher for a wrong answer, even if the learner says the rationale now makes sense. SM-2 already brings failures back tomorrow. If you want a "teach me this again in ten minutes" button, put the item on a same-day queue instead of inflating quality.

Applying the scheduler to a CNA question bank

A nurse aide practice bank is not a stack of isolated facts. Items cluster by domain — safety, infection control, resident rights, ADLs, transfers, emergencies — and a learner who is weak on gait belts needs more than whichever single item happens to be due.

Use SM-2 at the item level, then add a thin layer on top:

  1. Pull every due item, as the algorithm requires.
  2. If the due list is short, fill the session with new items from the learner's weakest domain, not with random leftovers from a topic they already handle well.
  3. Cap new items per day so a 400-item bank cannot bury the reviews that are actually due.
  4. After the session, show a domain report ("transfers: 4 of 9 correct") alongside the next due date. The report is for the human; the due dates stay per item.

Store scheduling state next to the attempt, using the same item_id and item_version you would use in a versioned question schema. If an item's wording changes substantially, start a fresh card. A rewritten stem is not the memory the old interval was measuring.

Do not hide the schedule. A simple line — "This item comes back in 6 days" — teaches the learner what the system is doing and reduces the urge to smash through the whole bank in one sitting. That habit is the thing spaced repetition is trying to prevent.

Limits worth being honest about

SM-2 is old. Newer schedulers such as FSRS model memory with more parameters and often need less babysitting, but they also need more review history before they shine. For an exam-prep app with a few hundred items and learners who may only study for two weeks, SM-2 is usually enough.

It will not fix a bad item. If two options are defensible, the algorithm will keep surfacing confusion. It will not tell a CNA candidate whether they are ready for a state skills test; a written practice bank only measures written practice. And it will not invent official exam weights. Use it to time the next review, then send people back to clear explanations, such as the rationales on CNA TestBank, to do the actual learning.

If you ship only one feature from this article, ship the card state and the due list. The mapping from clicks to q can be tuned later. The important decision is to treat each item as a memory you are willing to see again tomorrow, not as a deck to burn through once.

About the author

  • Original, nurse-reviewed CNA practice tests with rationales for U.S. nurse aide candidates. Independent prep — not official exam items.

No comments yet. Be the first to comment.