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;
}
@gowtham-kingxo
Copy link

Hey, don't we need to add i < itemsCount condition in line 23? https://gist.github.com/AjithPanneerselvam/c40d2daf36862fc1faabd915d79722ee#file-questiongroup-js-L23

However, I think the end survey object will save us. Just want to confirm with you dude.

Yaa nanu End survey irukunu dha vitten but better to add in case of bad data la?

@gowtham-kingxo
Copy link

Dude, the code is clean. Just did few reordering of statements and some formatting.

Unga amma mela sathiyama? Line 33 andha decrement panradhu manasu uruthudhu, any other idea?

@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