B
B
be_a_man2020-08-24 23:19:10
JavaScript
be_a_man, 2020-08-24 23:19:10

How to find the longest branch in a tree?

JavaScript.
There is a step by step form. You need to show how much is left to answer, depending on the answers.
5f441fd430357464446335.jpeg

Sample data. Nesting can be any.

const quest = [
  {
    title: 'q1',
    answers: [
      { title: 'a1-1' },
      {
        title: 'a1-2',
        next: [
          {
            title: 'q1-2-1',
            answers: [
              { title: 'a1-2-1-1' },
              { title: 'a1-2-1-2' }
            ]
          }
        ]
      },
      {
        title: 'a1-3',
        next: [
          {
            title: 'q1-3-1',
            answers: [
              { title: 'a1-3-1-1' },
              { title: 'a1-3-1-2' },
              {
                title: 'a1-3-1-3',
                next: [
                  {
                    title: 'q1-3-1-3-1',
                    answers: [
                      { title: 'a1-3-1-3-1-1' },
                      { title: 'a1-3-1-3-1-2' },
                      { title: 'a1-3-1-3-1-3' }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  },

  {
    title: 'q2',
    answers: [
      { title: 'a2-1' },
      { title: 'a2-2' },
    ]
  },

  {
    title: 'q3',
    answers: [
      { title: 'a2-1' },
      {
        title: 'a2-2',
        next: [
          {
            title: 'q2-2-1',
            answers: [
              { title: 'a2-2-1-1' },
              { title: 'a2-2-1-2' }
            ]
          }
        ]
      },
      {
        title: 'a2-3',
        next: [
          {
            title: 'q2-3-1',
            answers: [
              { title: 'a2-3-1-1' },
              { title: 'a2-3-1-2' }
            ]
          }
        ]
      }
    ]
  },
]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xmoonlight, 2020-08-24
@xmoonlight

No, if there is a branch at the current step with a different number of questions.
You can only show the percentage of the maximum possible number of answers in this thread:
[progress, in %] = 100*([passed number of questions in the current thread] / [maximum number of questions in the current thread])
To find the number of passed / remaining steps on a particular question ("node"), you need to follow from each "node" of the "tree" to the "root" of the "tree" (to the beginning of the test / survey) and count the number of connections and immediately mark the "edges" as much as possible -possible value of the "depth" of the given "branch" from the current "node".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question