How To Set The Order Of Iterations Within a Loop

Overview

This How-To will explain and show how you can set the order of iterations within a given loop chapter.

Add the needed Call-Back function to the ‘Advanced Scripts’

Add the following code to your advanced script’s box:
function OnCreateIterationOrder(inChapterIndex, inIterationOrder)

{

}

This function is a callback so is called by the runtime engine when a loop chapter is entered (while conducting a survey).
the 1st parameter, ‘inChapterIndex’ passes through the current chapters index that the surveyor is located, for example:

 

Note: The index is 4 in this case.

And the 2nd parameter, ‘inIterationOrder’ passes through the original iteration order of the loop, for example:
If chapter 1 is a loop that iterates 5 times, it will pass through: [1,2,3,4,5].

Add the relevant code to the Call-Back function in order to perform your logical script.

Configure your function to suit your logical scripts.
In the above example (screenshot) chapter 4 (Q11) iterates 5 times based on an answer scale.

Lets also assume that I have int he survey a prior question that is with variable name Q1 and has 5 answers (uses the same scale) and that I have set it to randomize the answers.
We want chapter to iterate in the same order as the answers of Q1 were we can write the call back to be:

function OnCreateIterationOrder(inChapterIndex, inIterationOrder)
{
    var retVal = inIterationOrder;
    if(inChapterIndex==4)
    {
        retVal = GetQuestionAnswersRandomOrder(Q1);
    }
    return retVal;
}

* this should be written in the function we wrote in Step 1, within the advanced scripts box, as following:

You can write any script that you wish in order set different loop iterations order with this method.

Tip: As Chapter indexes may move while you script (add additional chapters etc.) you can build a function that returns the chapter index from its name:

function GetCahpterIndexFromName(inName)
{
var retVal = -1;
var chapters = GetAllChapters();

for (var i=0; i<chapters.length; i++)
{
if (chapters[i].Name == inName)
{
retVal = chapters[i].Idx;
}
}
return retVal;
}

 

That’s it!

Was this article helpful?
5 out of 6 found this helpful
Have more questions? Submit a request

Comments

2 comments
  • I would to know how does the Q1 being setup? retVal =GetQuestionAnswersRandomOrder(Q1).

    Also I will have some criteria to do randomization in loops. What is the steps to do it?

    0
    Comment actions Permalink
  • in Q1, GetQuestionAnswersRandomOrder , return the randomize order of the question , for example if your question have 5 answers , and you use randomizeAnswers(currQuest) in script init of Q1 or use the option randomize answers/ topics (addvance tap). and the random order return [4,3,2,5,1], in the chapter 4 iterate in the same order.

    in the loop you have to set MAx iter 5 , or create a Scale with 5 items. to make match with the number of anwers of Q1. if you chapter have les than 5 or more, the script dont works and return the default order [1,2,3,4,5].

    But be careful, because if you add a chapter above the loop chapter, the index shifts, and wherever you have == 4 it will stop working until you update it.

    What you can do is create a global variable like Vars["yourChapt"] = CurrChapter.Idx in the start script of the chapter where the loop is located.

    Then, in the function, replace inChapterIndex == 4 and use Vars["yourChapt"] instead of 4.

    sorry for my english

    0
    Comment actions Permalink

Please sign in to leave a comment.