For a school project, I’m making a webapp that presents a dichotomous key.
First, it shows a list of possible items. The user picks one in their head. The app then presents the user with a question and buttons for Yes or No. A new question is asked based on the response to the previous one. This continues until only one item fits the user’s responses.
My question is how to store the data. My initial idea was a nested array. Here’s an example using fruits:
Options: Apple, Orange, Banana, Pear
0 - "Is it shaped like a sphere?" Initial question
1 - Yes response
0 - "Do you eat the peel/skin?" Subsequent question
1 - "Apple" Answer based on Yes response
2 - "Orange" Answer based on No response
2 - No response to initial
0 - "Is it yellow Subsequent question
1 - "Banana" Answer based on Yes response
2 - "Pear" Answer based on No response
But, this way seems like it could get pretty unwieldy with a lot of data. Is there a better way to structure this?
This has to be completely JS/HTML; I don’t want to use a database or anything like that.
I’d suggest using a table like this (technically, it can be an array of objects)
It is more flexible compared to nested objects or graphs because you can determine questions and their order dynamically, depending on what items are selected.