1

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?

Evert
  • 2,022
  • 1
  • 20
  • 29
  • One should treat plain objects as unordered sets of properties (even though since ES6 there is an insertion order with the newer methods). You should use a (nested) array if questions have an order. – trincot Jun 18 '19 at 05:24
  • @trincot Ah, thanks for the tip. I changed it now. – Evert Jun 18 '19 at 06:16
  • How would you indicate that after a certain answer to a grandchild-question the next question would be either one level up, or two levels up (i.e. at the root)? Just `null` would not make that distinction... – trincot Jun 18 '19 at 07:15
  • @trincot See the answer that I've posted, it seems to be working – Evert Jun 18 '19 at 08:23
  • OK, so must I understand you never have answers that should make not only skip the rest of the questions at the current level, but also those at the immediate parent level? Are you saying I should not look any further into your post? – trincot Jun 18 '19 at 08:42
  • 1
    Correct and correct. I appreciate your willingness to help though! – Evert Jun 18 '19 at 09:49

1 Answers1

1

Hmm, I may have fixed it with this code. It's been working okay so far.

getCurrentQuestion(questions, responses) {
  if (!responses.length || !questions.length) {
    return questions.length ? questions[0] : null
  }

  let response = responses.shift()
  let question

  do {
    question = questions.shift()
  } while (question.question !== response.question)

  let answer = question.answers[response.answer]

  return this.getCurrentQuestion(answer || questions, responses)
    || this.getCurrentQuestion(questions, responses)
}
Evert
  • 2,022
  • 1
  • 20
  • 29