Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ronaldo100's avatar

Quiz app with multiple possible answers (logical problem)

Hello!

This is as much a Laravel related question as it is a logical problem.

Even though I want to solve it explicitly in Laravel, I couldn't even think of a good way to do it in pseudo code.

Imagine a quiz app with quiz questions.

Example #1

Question: What are the names of our school's guinea pigs?

The correct answers would be:

Joe Doe
Jane Doe
Cool Piggy
Smiley Face

But what if the students enter the guinea pigs in a different order?

e.g.:

Smiley Face
Jane Doe
Cool Piggy
Joe Doe

This answer would still be correct, as it contains all the correct names, but how would I check it against entries in the database?

The moment the order is different to the entries in the DB, I am kind of lost.

It gets even more terrifying, where there are typos!

Imagine, a student misspelled: "Cool" in the name "Cool Piggy"

e.g. "Cl Piggy" - how would I then know what entry to check for in the DB ?

There is no way to find it!

Especially if the last name is also misspelled, like "Cl Pgy".

Or, even worse:

Imagine, a student would write "ABC Doe".

Should the program then proceed to point out, that it should be "Jane Doe" ? or "Joe Doe" ?

That gets even trickier if the student has typos in multiple names, like so:

XXiley XYZ
XXane XYZ
XXool XYZ
XXoe XYZ

To make it easier for you guys to answer and play around with it, I have put together a multi-dimensional array with demo data of possible CORRECT answers for three questions (two of which I have not talked about yet)

Problem: Names with 2 or 3 words in the same array (see third question in the array)

$correct_answers = array();

$correct_answers[0] = array('Joe Doe', 'Jane Doe', 'Cool Piggy', 'Smiley Face');
$correct_answers[1] = array('Sky Blue', 'Blue Sky', 'Miss Sky', 'Mister Blue');
$correct_answers[1] = array('aaa bbb ccc', 'ddd eee', 'fff ggg', 'hhh iii jjj');  // mixed two and three words

Thank you :)

Yours, Ronaldo

0 likes
2 replies
Lumethys's avatar

i dont see anything complicated with this apart from the misspell stuff, but you can just treat the answer with the wrong spelling as incorrect

Suppose you have 4 person as the "correct answer" then you assign a "point" for each one. For example they get 1 point for each "right name" they give, so the maximum is 4 points.

How to determine a name is "right", then? The easy way is to limit the scope: "Please write answer in the following order: First name - Last name".

Or, if you want to allow shuffle, you could do something like this:

correct = " Jane Doe"
input = " Doe Jane "

a = first word of input //Doe
b = 2nd word of input //Jane

reverse = b + " " + a

if (input != correct AND reverse != correct){
	return false
}

The misspell on the other hand, should be treated as incorrect, as i said. If you want to tolerate it, you had to bring in something like a full-text search engine. But what is the point of a "Quiz" if everyone type in the wrong answer and it is treated as right?

OussamaMater's avatar

I am assuming every question has its answers, so I will get the answers of a specific question.

$answers = Question::find($id)->answers;

Let's say you get the form he student has submitted and you store it, after validation of course, in $student_answers, now you can pass it to this method that will check if the answers match or no calling this method, that will define later

checkIfAnswersMatch($answers, $student_answers);

The method should be something like this

function checkIfAnswersMatch($answers, $student_answers): bool {
    foreach ($student_answers as $student_answer) {
        if(!in_array(standardizeAnswer($student_answer), $answers)){
            return false;
        }
    }

    return true;
}

Note that we have a function called standardizeAnswer this one will help you get the student answer to be as close as the real answer

function standardizeAnswer(string $answer): string {
    // 1. remove all whitespaces
    $answer = preg_replace('/\s+/', '', $answer);
    // 2. all to lowercase
    $answer = strtolower($answer);
    // 3. you can do some regex you define to accept a word if it matches 99% for example (so all the letters expect one?)

    return $answer;
}

Now, when creating your answers with the question, setup a mutator or an observer to transform the answers to lowercase, so now you avoid checking if it's uppercase or not (when the student answer is all lowercase, and the original answer is too all lowercase, that's an easier check for the misspelled uppercases), and you can take it even further by setting simple regex to return true if the word has all the letters or all of them except one letter.

This should be as close as possible, in this kind of quizzes you usually add hints* (from my personal experience playing in different platforms and submitting this kind of answers where you are free to type wherever), so you say, the answers should be 4 letters, or any kind of helpful hints, as you can't really control users minds, you will never get that 100% matching, so guide your users, and what's a quizz if there are not mistakes and all answers are accepted.

Please or to participate in this conversation.