Today I came across a pretty challenging survey which I wanted to share with you.
The survey was asking farmers the following:
1. How many Fields do you own?
[LOOP 1] (Runs on the number of fields owned)
2. What is the size of field number i ?
3. What kind of units is field number i measured in?
4. How many lots is field number i divided to?
Then there were 8 sub chapters, 1 for each Lot. (Maximum of 8 lots).
Each Lot Chapter had the same set of questions, for example:

So I just entered an entrance rule to each Lot Chapter saying:
AnswerIter(4,IterationIndex)>=*NUMBER OF THE LOT*
So it would correspond to the number of lots entered each iteration for the specific field.
Question 6 was a single choice question which its answers were:
And 6,7,8 had entrance rules corresponding to the answer of 6, (only one of 6,7,8 would be asked eventually)

In the end of the Field & Lot loop, I wanted to show a summary of all Fields, with their sizes, type of units, type of crops and their amounts for each lot, and a summary of each crop by units in total of all lots summed together – there the challenging part began.
At the end of the Field Loop, I added an Expression Question, containing the following script I wrote:
I used ‘Vars[]’ variables, which are global variables, in order to keep the data flexible and available outside of the loop itself, as I couldn’t know what would be the amount of fields entered, neither the amount of lots for each field and the specific crops selected – but I had to get this data somehow in order to summarize all of it.

The 1st ‘For’ (line 5) loop would run according to the number of fields, creating a variable for each field size, field units, and field number of lots.
The 2nd ‘For’ (line 10) loop would run according to the number lots, creating a variable for each lot size, lot units, and lot type of crop.
The 3rd ‘For’ (line 35) loop would run according to the number of fields, and write as text all of the data we collected to the ‘Summary’ variable.
The 4th ‘For’ (line 44) would run on all key’s (crop names) in my associative array containing the crops names and total for all of the fields, and write them as text to the variable called ‘Total’.

Eventually, I just took an empty question, put two place locators in it {0}, {1} and set the first one to display Vars[“Summary”] and the second to display Vars[“Total”].

  1. Vars[“NumOfFields”]=Answer(1);
  2. Vars[“Summary”]=“”;        //will store all the text for the summary
  3. var cropTotal = new Array;    //ascociative array, gets a key and a value
  4. Vars[“Total”]=“”;            //will store all the text for the sum’s of different lots with same crops
  5. for(var i=1; i<=Vars[“NumOfFields”]; i++)        //runs on number of fields
  6. {
  7. Vars[“Field”+i+“Size”]=AnswerIter(2,i);        //get size of field i
  8. Vars[“Field”+i+“Units”]=SelectedAnswerTextIter(3,i);    //get unit type of field i
  9. Vars[“Field”+i+“Lots”]=AnswerIter(4,i);    //get number of lots in field i
  10. for(var j=1; j<=Vars[“Field”+i+“Lots”]; j++)    //runs on all lots in field i
  11. {
  12. Vars[“Field”+i+“Lot”+j+“Size”]=AnswerIter(j*5,i);    //gets the size of lot j in parsel i
  13. Vars[“Field”+i+“Lot”+j+“Type”]=AnswerIter(6+(5*(j-1)),i);    //gets the the type of lot j in parsel i
  14. if(Vars[“Field”+i+“Lot”+j+“Type”]==1)    //if type is fruit
  15. {
  16. Vars[“Field”+i+“Lot”+j+“Crop”]=SelectedAnswerTextIter(7+(5*(j-1)),i,true);    //get the name of the crop
  17. }
  18. else if(Vars[“Field”+i+“Lot”+j+“Type”]==2)    //if type is vegteble
  19. {
  20. Vars[“Field”+i+“Lot”+j+“Crop”]=SelectedAnswerTextIter(8+(5*(j-1)),i,true);    //get the name of the crop
  21. }
  22. else if(Vars[“Field”+i+“Lot”+j+“Type”]==3)    //if type is grain
  23. {
  24. Vars[“Field”+i+“Lot”+j+“Crop”]=SelectedAnswerTextIter(9+(5*(j-1)),i,true);    //get the name of the crop
  25. }
  26. if (cropTotal.hasOwnProperty(Vars[“Field”+i+“Lot”+j+“Crop”]))    //check if crop in field i lot j exists
  27. {
  28. cropTotal[Vars[“Field”+i+“Lot”+j+“Crop”]]+=Vars[“Field”+i+“Lot”+j+“Size”];    //if yes, sum it.
  29. }
  30. else{
  31. cropTotal[Vars[“Field”+i+“Lot”+j+“Crop”]]=Vars[“Field”+i+“Lot”+j+“Size”];//if no create it in the array.
  32. }
  33. }
  34. }
  35. for(var i=1; i<=Vars[“NumOfFields”]; i++)    //loop that writes all information to the summary variable.
  36. {
  37. Vars[“Summary”]+=“Field”+i+“(“+Vars[“Field”+i+“Size”]+” “+Vars[“Field”+i+“Units”]+“)”+“”+“”;
  38. for(var j=1; j<=Vars[“Field”+i+“Lots”]; j++)
  39. {
  40. Vars[“Summary”]+=“Lot”+j+“(“+Vars[“Field”+i+“Lot”+j+“Size”]+Vars[“Field”+i+“Units”]+“)”+“”;
  41. Vars[“Summary”]+=“Crop:”+Vars[“Field”+i+“Lot”+j+“Crop”]+“”+“”;
  42. }
  43. }
  44. for(var key in cropTotal)        //summarize all the sum’s to the total variable
  45. {
  46. Vars[“Total”]+= key+“:”+cropTotal[key]+“”;
  47. }
  48. //p.s. Vars[“”] is a global variable that gets a key and a value
  49. return 0;        //just because expressions must return something, has no meaning

Eventually a summary might look like this:

While the questions body before the text piping would have looked like this:

And in it’s start script I have piped the global variables which I stored all text information in:

Visit us at: