I've been struggling to write the correct algorithm for this. So I have a tree of questions that looks like this:
[
{
question: "Question 1",
answers: {
"yes": [
{
question: "Question 1a",
answers: ["A", "B"]
}
],
"no": null
}
},
{
question: "Question 2",
answers: ["I agree"]
}
]
And I have a responses
object, which could look something like this:
[
{ question: "Question 1", answer: "yes" },
{ question: "Question 1a", answer: "B" },
]
Or it could be this:
[
{ question: "Question 1", answer: "no" },
]
And now the code I'm struggling to write is to find out what the next question should be. The above two examples of the responses
object should both point to the next question being "Question 2"
.
I'd like to have a function like getNextQuestion(questions, responses)
which would then hopefully output:
{
question: "Question 2",
answers: ["I agree"]
}
I've figured out how to go down the tree, but I can't figure out how to go up the tree when that's necessary. Anybody willing to help?