Back to writing

Teaching a library to say “Did you mean...?”

One mistyped letter can waste an evening. Here is how Nyx notices the typo and guesses what you meant.

Six letter blocks spell SUMBIT. The M and B float above the row, glowing, with arrows showing they should swap places, and a hand reaches in to move them.
Image © 2026 Chiranjit Karmakar. All rights reserved.

Here is a button and some jQuery. Clicking the button does nothing, and there is no error in the console. Can you see why?

<button id="submit-btn">Save</button>

<script>
  $("#sumbit-btn").on("click", save);
</script>

The m and the b are the wrong way round. jQuery found no elements, so it added your click handler to none of them. That’s by design, and it’s often convenient. But if you started programming last week, you may spend the evening looking for a bug inside save, which never ran.

Make the same mistake in Nyx, and this appears in the console:

Nyx: Nothing matches "#sumbit-btn".
  -> Did you mean "#submit-btn"?

It also adds the file and line number when it can work them out. There is no AI behind the message. It takes one old algorithm and some care about when to use it.

How far apart are two words?

To suggest a word, you first need a way to say how close two words are. Nyx counts the smallest number of single-letter edits that turn one into the other. An edit is adding a letter, removing one, or changing one.

You typed You meant Edits What changed
clik click 1 add c
cat cut 1 change a to u
sumbit submit 2 change m to b, and b to m

This number is called the Levenshtein distance, after Vladimir Levenshtein, who described it in the 1960s.

Working it out

There are many ways to turn one word into another, and we want the shortest. Trying them all would be far too slow. Instead, we solve small versions of the problem and build on them.

Make a grid with clik down the side and click along the top. Each cell answers one small question: how many edits turn the letters so far on the side into the letters so far on the top?

– c l i c k
– 0 1 2 3 4 5
c 1 0 1 2 3 4
l 2 1 0 1 2 3
i 3 2 1 0 1 2
k 4 3 2 1 1 1

The top row is easy: going from nothing to cli means adding three letters. The left column is just as easy, because going from cli to nothing means removing three. Every other cell is the smallest of three choices:

  • the cell above, plus 1, for removing a letter
  • the cell to the left, plus 1, for adding a letter
  • the cell diagonally up and left, plus 1 for changing a letter, or plus 0 if the letters already match

The bottom-right cell is the answer for the whole words: 1.

In code, you don’t need to keep the whole grid. Each row only looks at the row above it.

function distance(a, b) {
  let row = Array.from({ length: b.length + 1 }, (_, i) => i);
  for (let i = 1; i <= a.length; i++) {
    const next = [i];
    for (let j = 1; j <= b.length; j++) {
      const same = a[i - 1] === b[j - 1];
      next[j] = Math.min(
        row[j] + 1,                  // remove a letter
        next[j - 1] + 1,             // add a letter
        row[j - 1] + (same ? 0 : 1)  // change a letter, or keep it
      );
    }
    row = next;
  }
  return row[b.length];
}

The version in Nyx does the same thing with a single row, and turns both words to lower case first. I’ve used two rows here because they’re easier to follow.

Choosing the closest word

Now compare the typo with every word it could have been, and keep the one with the smallest distance.

function closest(word, choices) {
  let best = null;
  let bestScore = Infinity;
  for (const choice of choices) {
    const d = distance(word, choice);
    if (d < bestScore) {
      bestScore = d;
      best = choice;
    }
  }
  const allowed = Math.max(2, Math.floor(word.length / 3));
  return bestScore <= allowed ? best : null;
}

The last two lines matter most. Something is always the closest, even when it isn’t close at all. Without a limit, Nyx would answer every mistake with a confident wrong guess, and a wrong hint is worse than none. So it allows one edit for every three letters, and never fewer than two. sumbit-btn has ten letters, so it gets three edits, and it only needs two.

Why never fewer than two? Because swapping two neighbouring letters is such a common slip, and Levenshtein counts a swap as two edits.

If nothing is close enough, Nyx doesn’t guess. It tells you to check the spelling, or to run the code after the element exists.

Where do the choices come from?

For selectors, from your own page. When #sumbit-btn matches nothing, Nyx goes through every element and collects the ids, classes and tag names that are really there. The symbol in front says which list to use: ids for #, classes for ., and tags for neither. It can only suggest something that exists.

This work happens only when a search finds nothing, so a selector that matches doesn’t pay for it.

Event names come from a list inside Nyx, which is how .on("clik", ...) gets Did you mean "click"?. There is one exception: a name containing a dash or a colon, such as user-login, is left alone, because it’s probably an event you made up yourself.

Not every mistake is a spelling mistake

A very common beginner slip has perfect spelling:

nyx("#card") // but the page has class="card"

If Nyx only measured distance, it would search the ids, where the right answer isn’t. So before it looks for typos, it checks for the right name with the wrong symbol:

Nyx: No element has the id "card", but some have the class "card".
  -> Use ".card" instead of "#card".

It also notices a forgotten # or .. And before any of that, it checks whether the page is still loading, because then your spelling may be fine and your code is simply early.

The order of these checks matters. The distance function is the clever part, but good error messages depend more on listing the mistakes people really make, and testing for the likeliest first.

Where it still gets things wrong

Very short names are the weak spot. If your page has an element with the id map and you ask for #nav, Nyx says:

Nyx: Nothing matches "#nav".
  -> Did you mean "#map"?

You almost certainly didn’t. But nav and map are two edits apart, and two is always allowed, even when that’s most of the word. A limit that shrinks for short names would stop this, but then it would miss real swaps in them. Counting a swap as one edit, as a variant called Damerau–Levenshtein does, would let the limit be lower.

It also only checks simple selectors. For something like .list > .itm, it tells you nothing matched but doesn’t guess which part is wrong.

Try it

If you’re learning, build this yourself. Take the two functions above, give closest a list of city names, and type one badly. Then try to break it: use very short words, or two names equally far from your typo. Deciding what it should do then is the interesting part.

You can also see it working in the Nyx documentation, which has examples you can edit on the page. Misspell something on purpose. The code is on GitHub.