Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
How to get the number of all possible poll paths?

There is a quiz that shows a question depending on the answer. The JSON file contains the id of the questions that are displayed when you select.The question object looks like this:

const quiz = [
  {
    id: "1",
    question: "question1",
    answer1: {
      text: "answ1",
      nextQuestion: "2",
    },
    answer2: {
      text: "answ2",
      nextQuestion: "3",
    },
  },

  {
    id: "2",
    question: "question2",
    answer1: {
      text: "answ1",
      nextQuestion: "",
    },
    answer2: {
      text: "answ2",
      nextQuestion: "",
    },
  },

  {
    id: "3",
    question: "question3",
    answer1: {
      text: "answ1",
      nextQuestion: "",
    },
    answer2: {
      text: "answ2",
      nextQuestion: "",
    },
  },
];

How can you go through all the possible branches of the questionnaire and form such an object with data:

  {
    branches: {
      questionsList: [
        [
          { "question1": "answ1" },
          { "question2": "answ1" },
        ],
        [
          { "question1": "answ1" },
          { "question2": "answ2" },
        ],
        [
          { "question1": "answ2" },
          { "question3": "answ1" },
        ],
          [
          { "question1": "answ2" },
          { "question3": "answ2" },
        ],
      ],
    },
  },

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This may be one implementation to achieve the desired objective.

Code Snippet

const getAllBranches = arr => {
  // first create a map/object to quickly access each question by id
  const qMap = arr.reduce((f, i) => ({
    ...f,
    [i.id]: {...i, ch: [ // keep track of 'children' using this 'ch' array
      ...Object.keys(i)
      .filter(k => k.includes('answer')) // any key with 'answer' is included
      .map(k => ({...i[k]}))
    ]}
  }), {});
  
  // recursive function to get the desired objective
  const recurGet = ({id, question, ch}) => (
    ch.map(({text, nextQuestion}) => (
      nextQuestion.length === 0 // if no 'nextQuestion', simply return an object
      ? { [question] : text }
      : recurGet(qMap[nextQuestion]).map(ob => ({
        [question]: text,
        ...ob // if else, recursive-call and append question-answer to each result
      }))
    ))
  );
  
  return recurGet(qMap[arr[0].id]);
};

const quiz = [{
    id: "1",
    question: "question1",
    answer1: {
      text: "answ1",
      nextQuestion: "2",
    },
    answer2: {
      text: "answ2",
      nextQuestion: "3",
    },
  },

  {
    id: "2",
    question: "question2",
    answer1: {
      text: "answ1",
      nextQuestion: "",
    },
    answer2: {
      text: "answ2",
      nextQuestion: "",
    },
  },

  {
    id: "3",
    question: "question3",
    answer1: {
      text: "answ1",
      nextQuestion: "",
    },
    answer2: {
      text: "answ2",
      nextQuestion: "",
    },
  },
];

// add the branches into a new object to meet 
// the desired objective's structure
const resObj = {
  branches: {
    questionsList: getAllBranches(quiz)
  }
};
console.log(resObj);

Explanation

Relevant comments have been included in the code above.

Please post any questions on comments for further detailed explanation, if required.

NOTE: Please supply further test cases (such as with more than 2 answers, etc) to effectively test the solution.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!