!LOOP case - Change action every 5th loop

Hello,

I’m stuck with a simple challenge.

I run the macro in a loop to do an action (A) on a web page.

I need to build logic to check the iLoop and perform a different action (B) every fifth loop.

So action (a) will be done 4 loops, then action (B) the 5th loop, and so on.

Can I get some help please to get this right?

Easy, you can do a counter in JS but the easy way would be to write three macros. One for the 1st four loops and one for the 5th loop. The main macro would be simply:

run Macro1
run Macro1
run Macro1
run Macro1
run Macro2
1 Like

You can use Javascript to get the modulus (remainder). Then use if / else to run the code.

var rem = loopCtr % 5;

If rem = 0
Run the code for every 5th cycle
else
Run normal code
end

Sorry, I don’t have the correct syntax as I’m mobile

1 Like

If you place your code that you want to run instead of the echos, it will only run every five loops.

This script will count the loops until it hits the 5th loop where it will change to the five loop code, reset its count and start counting to five again until the script is stopped.

If you need to run anything before or after the code divergence, you are able to put it safely before or after my script.

{
  "Name": "for foroum",
  "CreationDate": "2019-12-4",
  "Commands": [
    {
      "Command": "store",
      "Target": "true",
      "Value": "!errorignore"
    },
    {
      "Command": "store",
      "Target": "${Number}",
      "Value": "Number"
    },
    {
      "Command": "if_v2",
      "Target": "${!LastCommandOK}",
      "Value": ""
    },
    {
      "Command": "else",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "store",
      "Target": "1",
      "Value": "Number"
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "store",
      "Target": "false",
      "Value": "!errorignore"
    },
    {
      "Command": "if_v2",
      "Target": "(${Number}==5)",
      "Value": ""
    },
    {
      "Command": "echo",
      "Target": "Place loop 5 code here",
      "Value": ""
    },
    {
      "Command": "store",
      "Target": "0",
      "Value": "Number"
    },
    {
      "Command": "gotoLabel",
      "Target": "SkipToLabel",
      "Value": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "echo",
      "Target": "Put the 4 loop code here",
      "Value": ""
    },
    {
      "Command": "label",
      "Target": "SkipToLabel",
      "Value": ""
    },
    {
      "Command": "storeEval",
      "Target": "(${Number} + 1)",
      "Value": "Number"
    }
  ]
}

I hope this helps!

Nice way to do it w/ flow control!