Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Logic dynamically

I search for reusable tool or algorithm that enable me to apply conditional logic to questions dynamically.

lets explain in details:

question 1 - What is your age?

a) less 18        
b)between 18-25     
c) greater than 25

if he choose

a) then he go to Question 2 
b) he will goto question 5 
c) he will goto question 7 

So as I said the next question depends on the answer of the current question.I don't need to set if condition for each question .I need it to be dynamically.

I hope it is clear now . Is there any component or design pattern or algorithm implements what I said

All ideas are welcomed.

like image 968
Mohammed Thabet Avatar asked Jul 28 '26 22:07

Mohammed Thabet


1 Answers

It sounds like you probably want a lookup table, effectively mapping the pair (input question, answer) to the next question number to ask. Perhaps it should default to "go to the next question" if there's no entry in the table.

Exactly how you represent it in data structures will depend on what you're using to store the questions. For example, in SQL you could have a table with columns of "input question, answer, next question". In C# you might have a Dictionary<Tuple<int, int>, int>... or possibly (if there aren't going to be huge numbers of questions) just a List<AnswerPath> where AnswerPath contains the same three values as the SQL table would have done. (Change the name, it's awful, but you get the idea.)

like image 115
Jon Skeet Avatar answered Jul 30 '26 10:07

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!