I want to make a quiz and the user should type the right answer.
Let’s say the answer is correct if the answer matches 90%. For example, if the user types
Britney Spers instead of Britney Spears, the answer should be right.
I searched for Javascript functions to determine how accurate the answer is, I found some interesting functions for PHP, Ruby etc, but I need it in JavaScript.
Has anybody experience with these kind of algorhitms?
Thank you if you answer 🙂
You’re looking for the edit distance (aka Levenshtein distance). Under this scheme, the distance between two strings is the number of insertions, deletions, or substitutions required to make the strings match. For example, if the right answer is “oranges”, then:
s)r, substitutes -> r)o -> s, substituter -> p, substituteo -> a)oranges)A simple algorithm for it in Javascript would look like this (adapted and modified from this gist):
One problem with your question is that the examples you wrote about what you’re looking for (e.g. “matches 90%” or “accuracy of the answer”) are not well-defined metrics.
There are a lot of ways an answer can be wrong. For example, let’s say the right answer is “apple”. Which of these should be accepted?
and so on. Deciding which of these should be accepted is beyond the power of a simple edit-distance algorithm and will require heavier lifting, like NLP.