3/5

a!localVariables(): compare every score to the average

Medium

Return one label per score: "above" when the score is greater than the average of the whole list, "below" otherwise.

The average has to be worked out before the loop starts and held in a local. Calling average() inside the loop would recompute it for every single item.

A score exactly equal to the average counts as below.

Input

  • ri!scores — List of Integer

ri! Rule Inputs

ri!scoresList of Integer
{
  10,
  20,
  30
}

Example 1 — One above the average

Input: ri!scores
{
  10,
  20,
  30
}
Output:
{
  "below",
  "below",
  "above"
}

Example 2 — Everything equal counts as below

Input: ri!scores
{
  5,
  5
}
Output:
{
  "below",
  "below"
}

Example 3 — A single score is its own average

Input: ri!scores
{42}
Output:
{"below"}

Example 4 — Two apart

Input: ri!scores
{
  0,
  100
}
Output:
{
  "below",
  "above"
}

Appian Expression Tips

  • • Use ri!variableName for rule inputs
  • • Use a!localVariables(local!x: …, body) for locals
  • • Use a!forEach(items: …, expression: fv!item) to iterate
  • • Use where() / reject(fn!isnull, list) to filter