Need help on expression rule for array calculation

Certified Senior Developer

I am having an array like {22,10,12,11, 8, 17}. I need an expression which return the index where individual array value or sum of previous values is greater than or equal to 20.

For example: The output should be {1, 3, 5, 6}.

22 (22-20) => 2 (index = 1)

2 (remainder from previous value) + 10 + 12 (24-20) => 4 (index = 3)

4 (remainder from previous value) + 11 + 8 (23-20) => 3 (index = 5)

3 (remainder from previous value) + 17(20-20) => 0 (index = 6) 

As there is no while/do-while loops in Appian so it seems bit difficult to get the desired output with foreach loop.

  Discussion posts and replies are publicly visible

Parents Reply
  • 0
    Certified Senior Developer
    in reply to ranjank0002

    Not the most optimized solution, just gave it a try:

    -Expression Rule Name: DA1_trialCommunity

    -Rule Inputs: 

    arr: List of Number(Integer)
    prev: Number(Integer)
    ans: List of Number(Integer)
    index: Number(Integer)

    if(
      a!isNullOrEmpty(ri!arr),
      ri!ans,
      a!localVariables(
        local!sum: ri!prev + ri!arr[1],
        local!rem: if(
          local!sum >= 20,
          local!sum-20,
          local!sum
        ),
        rule!DA1_trialCommunity(
          arr: ldrop(ri!arr,1),
          prev: local!rem,
          index: ri!index+1,
          ans: if(
            local!rem <> local!sum,
            append(ri!ans, ri!index),
            ri!ans
          )
        )
      )
    )

    You can further optimize it and also use reduce function.

Children