AMPscript FOR LOOP Challenge

Process Loops, are, awesome.
However I often see loops treated as a developer-level function and they are avoided by marketing teams – which is a shame. So to help make loops more approachable to beginners, I’ve made an introductory video and developed a simple learning challenge that will help users to understand the power of loops!

To complete this challenge you will need to use multiple Loops and comparison statements to produce a desired output. Follow the task list below to complete this challenge so that the final output is as follows:

Challenge Tasks:

The 6 tasks below are progressive code challenges that will build up to the final result (shown above).
Read each task description (left) and try to replicate the results shown (right).

I recommend attempting this code challenge in a Cloud Page and checking your result in the Preview screen; it’s going to be the fastest way to develop and test your code during these tasks!

SPOILER ALERT! I’ve provided a solution to each task at the bottom of this page – so don’t scroll down too far!

1: Using a FOR LOOP, create a comma separated string of characters from 1 to 10.

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

2: Using a table with a 1px border, change your code so that the 10 numbers are placed into 10 cells on 1 row of a table.

3: Create a 2nd FOR LOOP to produce a 10×10 grid, counting up to 100.

Tip: You’ll need to use a 2nd FOR LOOP to achieve this!

4: Style the grid so that every 2nd Row has the Background Colour “#DCDCDC” (Light Grey) as shown.

5: In addition to the previous style, make every 2nd Column have the “#DCDCDC” Background Colour.
However, when both Row & Column are “#DCDCDC”, make the cell background “#C3C3C3” (Dark Grey).

6: Style the grid so that every multiple of 3 is “#FFFF00″(Yellow), and if the cell is already “#C3C3C3” (DarkGrey), then make it “#FFA500” (Orange); as shown.

Answers

Try to complete the challenge above before reviewing the answers. Note that is more than 1 way to solve each of these tasks, and the code below is just one of the possible solutions.

Task 1:
%%[
FOR @i = 1 TO 10 DO
Output(Concat(@i,IIF(@i==10,"",", ")))
NEXT @i]%%
Task 2:
<table border="1"><tr>
%%[FOR @i = 1 TO 10 DO]%%
<td>%%=v(@i)=%%</td>
%%[NEXT @i]%%
</tr></table>
Task 3:
<table border="1">
%%[FOR @x = 0 TO 9 DO]%%
<tr>
%%[FOR @i = 1 TO 10 DO]%%
<td>%%=v(Add(Multiply(@x,10),@i))=%%</td>
%%[NEXT @i]%%
</tr>
%%[NEXT @x]%%
</table>
Task 4:
<table border="1">
%%[FOR @x = 0 TO 9 DO]%%
<tr>
%%[FOR @i = 1 TO 10 DO
SET @color =
IIF(MOD(@x,2)==0,"#FFFFFF","#DCDCDC")]%%
<td style="background-color:%%=v(@color)=%%;">%%=v(Add(Multiply(@x,10),@i))=%%</td>
%%[NEXT @i]%%
</tr>
%%[NEXT @x]%%
</table>
Task 5:
<table border="1">
%%[FOR @x = 0 TO 9 DO]%%
<tr>
%%[FOR @i = 1 TO 10 DO
SET @color = IIF(MOD(@i,2)==0,
IIF(MOD(@x,2)==0,"#DCDCDC","#C3C3C3"),
IIF(MOD(@x,2)==0,"#FFFFFF","#DCDCDC")
)]%%
<td style="background-color:%%=v(@color)=%%;">%%=v(Add(Multiply(@x,10),@i))=%%</td>
%%[NEXT @i]%%
</tr>
%%[NEXT @x]%%
</table>
Task 6:
<table border="1">
%%[FOR @x = 0 TO 9 DO]%%
<tr>
%%[FOR @i = 1 TO 10 DO
IF MOD(Add(Multiply(@x,10),@i),3)==0 THEN
IF MOD(@i,2)==0 AND MOD(@x,2)==1 THEN
SET @color = "#FFA500"
ELSE
SET @color = "#FFFF00"
ENDIF
ELSE
IF MOD(@i,2)==0 THEN
IF MOD(@x,2)==0 THEN
SET @color = "#DCDCDC"
ELSE
SET @color = "#C3C3C3"
ENDIF
ELSE
IF MOD(@x,2)==0 THEN
SET @color = "#FFFFFF"
ELSE
SET @color = "#DCDCDC"
ENDIF
ENDIF
ENDIF]%%
<td style="background-color:%%=v(@color)=%%;">
%%=v(Add(Multiply(@x,10),@i))=%%
</td>
%%[NEXT @i]%%
</tr>
%%[NEXT @x]%%
</table>

Conclusion

Loops are a great way to introduce programmatic content into your communications, and I hope this AMPscript Code Challenge has helped you to understand how to use Loops. I don’t expect you’ll have much use for a number grid in your emails, however I’m sure the output you’ve managed to create has inspired you to think about AMPscript code differently!

Comments are closed.