2

i have questions i'm trying to iterate through , and every question might have subquestions , i wanna iterate through them all:- my variables are:-

                    Questions:[],
                    current_question : [],
                    check_box_answers: [],
                    got_subquestion: false,
                    sub_Questions :[],
                    iterator :  0,
                    subQuestion_iterator:-1,

Those are my data object:- and here is my function when i submit the value i wanna get the next question and check if it has subquestions or not, if it does, then iterate through the sub questions and when they finish get back to the main questions, here is what the json data look like:-

 {

            "title": "asd",
            "sub_questions": [],
            "section_title": "asdaa",
            "type": "radio",
            "answers": [
                {
                    "uuid": "56907C80-FD7D-4F83-8C25-3FBA1CD9A060",
                    "title": "1-3 asd"
                },
                {
                    "uuid": "A71EF3F5-2F02-44A2-ABC9-085D52AB450E",
                    "title": "4-10"
                },
                {
                    "uuid": "67EF9833-D39D-4A07-9974-6B1926474AF2",
                    "title": "asd 10"
                }
            ]
        },
        {
            "uuid": "D6F7785B-163E-4EFF-8F79-EE01F579E2A2",
            "title": "asdsda",
            "sub_questions": [
                {
                    "uuid": "8995B5A7-E698-47EF-9ADE-8107EAA13A16",
                    "title": "asdda",
                    "section_title": "dasdsa",
                    "type": "radio",
                    "answers": [
                        {
                            "uuid": "413DA1A7-2B44-4B06-9713-FB2A0020392F",
                            "title": "asda"
                        },
                        {
                            "uuid": "056ADC3A-C528-4615-9272-19EFAB73013F",
                            "title": "asdsda"
                        },
                        {
                            "uuid": "419B7C68-1032-448F-97EE-8A361605C693",
                            "title": "asdsdasda"
                        },
                        {
                            "uuid": "DC60E085-EDAB-49E1-B11A-A13C423B08B8",
                            "title": "asdsdad"
                        }
                    ]
                }

my function looks like this:-

 getNextQuestion(){
              var app = this
              // check if got_matrix = false
              if (app.got_matrix === false) {
                app.iterator = app.iterator+1
                console.log('iterator = ' + app.iterator)
                app.current_question = app.Questions[app.iterator]

               if (app.current_question.sub_questions.length > 0) { 
                  // first time matrix occurs
                  console.log('first time matrix occurs')
                  app.got_matrix = true
                  app.sub_Questions = app.current_question.sub_questions
                  console.log(app.sub_Questions)

                }

              }

              if (app.got_matrix === true) {
                // we have sub_questions
                console.log(app.sub_Questions.length)
                if (app.subQuestion_iterator+1 === app.sub_Questions.length ) {
                  // we are done iterating through all the matrix
                  console.log('no more sub_Questions')
                  app.got_matrix = false
                  app.subQuestion_iterator = 0
                  // get the next normal question
                  app.iterator = app.iterator+1;
                  app.current_question = app.Questions[app.iterator]
                }else{

                  // one more sub question 
                  console.log('one more sub question ')
                  app.subQuestion_iterator = app.subQuestion_iterator+1
                  console.log('sub_iterator = ' + app.subQuestion_iterator)
                  app.current_question = app.sub_Questions[app.subQuestion_iterator]

                }
              }

it's missing some questions, where did i mess up?

0

1 Answer 1

1

How about flattening all the questions into a linear form?

const flatten = (collection, property) => (collection || []).reduce((accumulator, item) => (
    accumulator.concat(item, flatten(item[property]))
), []);


const flattenedQuestions = flatten(yourQuestions, 'sub_questions');

Now getting the next question is as simple as bumping your app.iterator:

const app = {
  iterator: 0,
};

const getNextQuestion = index => flattenedQuestions[index];


getNextQuestion(app.iterator++);
getNextQuestion(app.iterator++);
getNextQuestion(app.iterator++);
Sign up to request clarification or add additional context in comments.

2 Comments

it returns this error " (collection || []).reduce is not a function " can you please check it out ? or put it on a codePen or any executable place/file??
My answer is error-free and bumps you in the right direction. collection is whatever you pass into flatten(). It should be an array, not an object. Your app.Questions should be an array of question objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.