Skip to content

Instantly share code, notes, and snippets.

@AjithPanneerselvam
Last active November 3, 2019 15:07
Show Gist options
  • Save AjithPanneerselvam/c40d2daf36862fc1faabd915d79722ee to your computer and use it in GitHub Desktop.
Save AjithPanneerselvam/c40d2daf36862fc1faabd915d79722ee to your computer and use it in GitHub Desktop.
const createQuestionGroups = (questionFlow) => {
const itemTypeQuestion = 'question'
const itemTypeBranch = 'branch'
const questionGroups = [];
const itemsCount = questionFlow.length;
let questionGroupCount = 1;
let i = 0;
while (i < itemsCount) {
let item = questionFlow[i];
if (item.type === itemTypeBranch) {
const branchDepth = item.depth;
let itemOrder = 1;
const questionGroupItems = [];
while(questionFlow[i+1].type == itemTypeQuestion && questionFlow[i+1].depth == (branchDepth + 1)) {
i += 1
item = questionFlow[i];
const questionGroupItem = {
order: itemOrder,
id: item.id
};
questionGroupItems.push(questionGroupItem);
itemOrder += 1;
item = questionFlow[i];
}
if (questionGroupItems.length > 1) {
const questionGroup = {
title: `Question Group ${questionGroupCount}`,
id: uuid(),
questions: questionGroupItems,
};
questionGroups.push(questionGroup);
questionGroupCount += 1;
}
}
i += 1;
}
@AjithPanneerselvam
Copy link
Author

If you are badly worried about it, then see how this looks

while(questionFlow[i+1].type == itemTypeQuestion && questionFlow[i+1].depth == (branchDepth + 1)) {
                i += 1
                item = questionFlow[i];

                const questionGroupItem = {
                    order: itemOrder, 
                    id: item.id 
                };
                
                questionGroupItems.push(questionGroupItem);
                itemOrder += 1;
                item = questionFlow[i];
}

Also edited in the source code - https://gist.github.com/AjithPanneerselvam/c40d2daf36862fc1faabd915d79722ee#file-questiongroup-js-L19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment