-
Notifications
You must be signed in to change notification settings - Fork 60
Nested Loops
Nested loops are not treated any differently than a regular loop. All the values of an inner loop are stored in a single array, so this means you need to keep track of how many times an inner loop runs for each outer loop iteration.
It's a good idea to diagram out the general flow of the interview process before you begin.
In the diagram above, the questions q2
, q3
, q4
, and q5
will be the outer loop, q6
and q7
will be the inner loop.
In order for LogicPull to keep track correctly of all variables inside the loop, you must name the outer loop and inner loop differently.
Set the "Loop" to "outer" for q2
, q3
, q4
, and q5
, and set the "Loop" to "inner" for q6
and q7
.
Before entering a loop, you must initialize a loop counter variable. In q1
, we assign a variable outer_loop
to 0 and inner_loop
to 0. The inner_loop
variable needs to be initialized outside any loops, and will be reset to 0 before each iteration of the inner loop. A variable called total_inner_loop_count
is also initialized to 0. It is used to keep track of the total number of iterations of the inner loop for all outer loop iterations. For example, if the outer loop runs twice, and the inner loop runs twice each time, then total_inner_loop_count
would be 4.
The outer loop needs to track how many times the inner loop ran. The reason for this because LogicPull stores all answers from a loop in a single array, so if we are visiting the inner loop multiple teams we need a way to know which portion of the array corresponds with the outer loop iteration. We use a inner_loop_tracker
variable to store this data. It is important that this variable is declared inside the outer loop, so it's values correspond to each iteration of the outer loop.
For each iteration of the inner loop, not only do we need to increment the inner_loop
variable, but also the total_inner_count
variable. It is this total inner count that is set to the inner_loop_tracker
on each outer iteration.
Here is the interview from this tutorial.
This is an example stylesheet of how you can reference the fields collected in the loops.
<?xml version="1.0"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="Out" page-width="8.5in" page-height="11in" margin-top="1in" margin-bottom="0.5in" margin-left="0.75in" margin-right="0.75in">
<fo:region-body margin-top="1in" margin-bottom="0.25in"/>
<fo:region-before extent="1in"/>
<fo:region-after extent="0.25in"/>
<fo:region-start extent="0in"/>
<fo:region-end extent="0in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="Out" initial-page-number="1" force-page-count="no-force">
<fo:static-content flow-name="xsl-region-before">
<fo:block font-size="14pt" text-align="center">Sample for Nested Looping</fo:block>
</fo:static-content>
<fo:flow flow-name="xsl-region-body" font-size="10pt">
<!--
Main Loop
The outer loop is the first for loop. We intialize a counter
i to hold the outer loop count, and a counter k that is used
to keep a running pointer to the inner loop index. For example,
if the first outer loop iteration has two inner loop iterations,
the next iteration of the outer loop needs to know to start
the inner loop from where it left off last iteration.
-->
<% for (var i = 0, k = 0; i <= master['outer_loop'].getAnswer(); i+=1) { %>
<fo:block font-size="11pt">
<fo:inline font-weight="bold" text-decoration="underline">Outer Loop Iteration <%= i %> </fo:inline>
</fo:block>
<fo:block><fo:leader /></fo:block>
<fo:block start-indent="15pt" padding-left="10pt" margin-left="10pt">
<fo:inline font-weight="bold">Outer Loop Data:</fo:inline>
<fo:inline><%= master['q2_0'].getAnswer()[i] %></fo:inline>
</fo:block>
<fo:block><fo:leader /></fo:block>
<!--
Go through the inner loop. The inner_loop_tracker
variable we set is used here to find out how many
times the inner loop ran. The counter we initialized
in the first loop (k) is incremented each iteration
so it points to the correct value in the inner loop.
-->
<% for (var j = 0; j <= master['inner_loop_tracker'].getAnswer()[i]; j+=1, k+=1) { %>
<fo:block start-indent="35pt" padding-left="20pt" margin-left="20pt">
<fo:inline font-style="italic" text-decoration="underline">Inner Loop Iteration <%= j %></fo:inline>
</fo:block>
<fo:block start-indent="55pt" padding-left="30pt" margin-left="30pt">
<fo:inline font-weight="bold">Inner Loop Data:</fo:inline>
<fo:inline><%= master['q6_0'].getAnswer()[k] %></fo:inline>
</fo:block>
<fo:block><fo:leader /></fo:block>
<% } %> <!-- inner loop for -->
<fo:block><fo:leader /></fo:block>
<% } %> <!-- outer loop for -->
</fo:flow>
</fo:page-sequence>
</fo:root>