Last active
November 3, 2019 15:07
-
-
Save AjithPanneerselvam/c40d2daf36862fc1faabd915d79722ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
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?
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
Yaa nanu End survey irukunu dha vitten but better to add in case of bad data la?