Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

195
Visualizações
How to count all possible paths of a questionnaire?

I have a questionnaire in which subsequent questions depend on the user's answer. I need to count the number of possible questionnaire paths, find out the number of paths and add all the questionnaire paths to the array. How can i do this?

In my code, I change the structure of the questions and add an array with answers and follow-up questions:

const newQuestionObj = {};

const getAllPath = (arr) => {
  const treeStructure = arr.forEach((item) => {
    newQuestionObj[item.id] = {
      ...item,
      children: [
        ...Object.keys(item)
        .filter((k) => k.includes("answer"))
        .map((k) => ({ ...item[k]
        })),
      ],
    };
  });
  console.log(newQuestionObj);
};

getAllPath(questions);
<script>
  const questions = [{
      id: "1",
      question: "q1",
      answer_1: {
        text: "a1",
        next_question: "2",
      },
      answer_2: {
        text: "a2",
        next_question: "3",
      },
    },
    {
      id: "2",
      question: "q2",
      answer_1: {
        text: "a1",
        next_question: "",
      },
      answer_2: {
        text: "a2",
        next_question: "",
      },
    },
    {
      id: "3",
      question: "q3",
      answer_1: {
        text: "a1",
        next_question: "",
      },
      answer_2: {
        text: "a2",
        next_question: "4",
      },
    },
    {
      id: "4",
      question: "q4",
      answer_1: {
        text: "a1",
        next_question: "",
      },
      answer_2: {
        text: "a2",
        next_question: "",
      },
    },
  ];
</script>

I need to get an object like this:

{
  paths: {
    number: 3,
    list: [
      [{
          "q1": "a1"
        },
        {
          "q2": "a1/a2"
        }
      ],
      [{
          "q1": "a2"
        },
        {
          "q3": "a1"
        }
      ],
      [{
          "q1": "a2"
        },
        {
          "q3": "a2"
        },
        {
          "q4": "a1/a2"
        }
      ],
    ]
  }
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda