Using Appian expressions, how would you generate a numeric triangle pattern?
Answer
You can nest one a!forEach() inside another to print patterns with several rows.
Appian Code
Explanation
Step 1: enumerate(5) + 1
The pattern needs one item per row. enumerate(5) generates five numbers starting from zero:
Adding 1 gives:
These numbers are the five rows.
Step 2: The outer a!forEach()
The outer loop takes one number at a time, and fv!item holds the current one: 1 on the first iteration, 2 on the second, and so on up to 5. The outer loop decides which row is being printed.
Step 3: The inner a!forEach()
The inner loop uses the outer loop’s current value to decide how many numbers to generate. When the outer value is 3, enumerate(3) + 1 generates {1, 2, 3}, and the inner loop returns each of those numbers.
Inside the inner loop, fv!item refers to the inner loop’s current item, not the outer loop’s item. The inner loop therefore counts from 1 up to the outer value instead of repeating the outer value.
Outer
| Inner output |
|---|---|
1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
Step 4: joinarray(..., " ")
joinarray() combines multiple values into one text string using a separator:
It turns each inner array into a space-separated string:
Inner array | After
|
|---|---|
|
|
|
|
|
|
|
|
|
|
Without joinarray(), the inner loop would return an array of values instead of a single formatted string for each row.
Final output
The outer loop generates five rows, and the inner loop generates the numbers in each row:
Printing triangle star pattern
The star pattern uses the same structure.
The outer loop still produces the row numbers 1 to 5, and the inner loop still runs once per unit of the row number. The only change is that the inner loop returns "*" instead of the current number. When the outer value is 3, the inner loop runs three times and returns three stars, and joinarray() joins them with spaces into * * *.
The tables for the star version look like this:
Outer
| Inner output | After
|
|---|---|---|
1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
Final star pattern
For more coding challenges try : https://appianverse.com/coding
Try it yourself
Change the inputs or the expression and run it to see what it returns.
Independent evaluator: it approximates the Appian expression language and may differ from a real Appian environment. Verify anything that matters. Terms
