# Loop increment and decrement

**URL:** <https://discourse.nodered.org/t/loop-increment-and-decrement/25161>\
**Category:** General\
**Created:** [19 April 2020 23:57 UTC](https://discourse.nodered.org/t/loop-increment-and-decrement/25161 "2020-04-19T23:57:23Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrizkywiguna](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrizkywiguna/32/32016_2.png) [@mrizkywiguna](https://discourse.nodered.org/u/mrizkywiguna)\
**Post date:** [19 April 2020 23:57 UTC](https://discourse.nodered.org/t/loop-increment-and-decrement/25161/1 "2020-04-19T23:57:23Z")

</div>

Hi All,

I create node to loop an auto increment then decrement. But my code didn't works. Here is my code:

```auto
[{"id":"f0e1fc36.bea","type":"debug","z":"d67945fe.28ff9","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1089,"y":1024,"wires":[]},{"id":"828e0993.766d1","type":"function","z":"d67945fe.28ff9","name":"","func":"var max = 100;\nvar min = 1;\nvar x = msg.payload;\nvar step = 1;\n\nsetInterval(function() {\n console.log(x);\n x += step;\n if (x === max || x === min) {\n step = -step;\n }\n}, 150);\nmsg.payload = x;\nreturn msg;","outputs":1,"noerr":0,"x":806.5,"y":1058,"wires":[["f0e1fc36.bea"]]},{"id":"234a16e.ee0f8ea","type":"inject","z":"d67945fe.28ff9","name":"","topic":"","payload":"1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":606.5,"y":1064,"wires":[["828e0993.766d1"]]}]

```

Thank you for any advice

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [19 April 2020 23:59 UTC](https://discourse.nodered.org/t/loop-increment-and-decrement/25161/2 "2020-04-19T23:59:34Z")

</div>

What are you wanting to happen?

You inject something (anything) and it starts counting 1 to 100 then 100 to 1?  
Repeating....

Looking at the code, I think it is way above my skill set.

Sorry.

Hope some one else can help. I don't think I can.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [20 April 2020 00:52 UTC](https://discourse.nodered.org/t/loop-increment-and-decrement/25161/3 "2020-04-20T00:52:14Z")

</div>

So, what do you see in your console? You can change the console.log() to a node.warn() and see it in the debug. I'd also suggest changing the loop from 150 to 15 for testing.'

When you see what you get, ask yourself 'Why is this happening?" and think about what you are seeing and why you see what you see.

---

<div class="post-metadata">

**Author:** ![mrizkywiguna](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrizkywiguna/32/32016_2.png) [@mrizkywiguna](https://discourse.nodered.org/u/mrizkywiguna)\
**Post date:** [20 April 2020 01:17 UTC](https://discourse.nodered.org/t/loop-increment-and-decrement/25161/4 "2020-04-20T01:17:18Z")

</div>

Yes, I want to start counting from 1 to 100 then 100 to 1.

---

<div class="post-metadata">

**Author:** ![mrizkywiguna](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrizkywiguna/32/32016_2.png) [@mrizkywiguna](https://discourse.nodered.org/u/mrizkywiguna)\
**Post date:** [20 April 2020 01:18 UTC](https://discourse.nodered.org/t/loop-increment-and-decrement/25161/5 "2020-04-20T01:18:44Z")

</div>

Hi @zenofmud

Thank you in advance for your advice. I've try your advice, and let me fix it.

---

<div class="post-metadata">

**Author:** ![mrizkywiguna](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrizkywiguna/32/32016_2.png) [@mrizkywiguna](https://discourse.nodered.org/u/mrizkywiguna)\
**Post date:** [20 April 2020 01:39 UTC](https://discourse.nodered.org/t/loop-increment-and-decrement/25161/6 "2020-04-20T01:39:40Z")

</div>

Here is an update for working code:

```auto
[{"id":"64ee5699.091968","type":"debug","z":"d67945fe.28ff9","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1162,"y":941,"wires":[]},{"id":"82abd753.035aa8","type":"function","z":"d67945fe.28ff9","name":"","func":"var max = 100;\nvar min = 0;\nvar x = 1;\nvar step = 1;\n\nsetInterval(function() {\n node.warn(x);\n x+=step;\n \n if (x ===max || x===min ) {\n step=-step;\n }\n}, 10);\n","outputs":1,"noerr":0,"x":879.5,"y":975,"wires":[["64ee5699.091968"]]},{"id":"e7f52d63.6fd2d8","type":"inject","z":"d67945fe.28ff9","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":689.5,"y":981,"wires":[["82abd753.035aa8"]]}]

```

---

<div class="post-metadata">

**Author:** ![homeplayer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/homeplayer/32/12388_2.png) [@homeplayer](https://discourse.nodered.org/u/homeplayer)\
**Post date:** [20 April 2020 14:33 UTC](https://discourse.nodered.org/t/loop-increment-and-decrement/25161/7 "2020-04-20T14:33:20Z")

</div>

added simple start stop to the flow

```auto
[{"id":"a27c7af5.cabff8","type":"debug","z":"4af2551d.b2e1ac","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":710,"y":580,"wires":[]},{"id":"898a365c.2acfe8","type":"function","z":"4af2551d.b2e1ac","name":"","func":"var max = 100;\nvar min = 0;\nvar x = 1;\nvar step = 1;\n\nsetInterval(function() {\n if(flow.get(\"run\") === false) {\n return null;\n }\n node.warn(x);\n x += step;\n \n if (x ===max || x===min ) {\n step=-step;\n }\n}, 150);\n","outputs":1,"noerr":0,"x":510,"y":580,"wires":[["a27c7af5.cabff8"]]},{"id":"92f7f172.70581","type":"inject","z":"4af2551d.b2e1ac","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":560,"wires":[["79de5eb3.0d7be"]]},{"id":"9984c439.32ea78","type":"inject","z":"4af2551d.b2e1ac","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":620,"wires":[["79de5eb3.0d7be"]]},{"id":"79de5eb3.0d7be","type":"change","z":"4af2551d.b2e1ac","name":"","rules":[{"t":"set","p":"run","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":580,"wires":[["898a365c.2acfe8"]]}]

```

---

<div class="post-metadata">

**Author:** ![mrizkywiguna](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrizkywiguna/32/32016_2.png) [@mrizkywiguna](https://discourse.nodered.org/u/mrizkywiguna)\
**Post date:** [6 May 2020 02:17 UTC](https://discourse.nodered.org/t/loop-increment-and-decrement/25161/8 "2020-05-06T02:17:55Z")

</div>

> [@homeplayer](#):
>
> [{"id":"a27c7af5.cabff8","type":"debug","z":"4af2551d.b2e1ac","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":710,"y":580,"wires":},{"id":"898a365c.2acfe8","type":"function","z":"4af2551d.b2e1ac","name":"","func":"var max = 100;\nvar min = 0;\nvar x = 1;\nvar step = 1;\n\nsetInterval(function() {\n if(flow.get("run") === false) {\n return null;\n }\n node.warn(x);\n x += step;\n \n if (x ===max || x===min ) {\n step=-step;\n }\n}, 150);\n","outputs":1,"noerr":0,"x":510,"y":580,"wires":[["a27c7af5.cabff8"]]},{"id":"92f7f172.70581","type":"inject","z":"4af2551d.b2e1ac","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":560,"wires":[["79de5eb3.0d7be"]]},{"id":"9984c439.32ea78","type":"inject","z":"4af2551d.b2e1ac","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":620,"wires":[["79de5eb3.0d7be"]]},{"id":"79de5eb3.0d7be","type":"change","z":"4af2551d.b2e1ac","name":"","rules":[{"t":"set","p":"run","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":580,"wires":[["898a365c.2acfe8"]]}]

Looks Better than me! Thanks @homeplayer

---

<div class="post-metadata">

**Author:** ![mrizkywiguna](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrizkywiguna/32/32016_2.png) [@mrizkywiguna](https://discourse.nodered.org/u/mrizkywiguna)\
**Post date:** [6 May 2020 03:25 UTC](https://discourse.nodered.org/t/loop-increment-and-decrement/25161/9 "2020-05-06T03:25:37Z")

</div>

Hi, my stupid questions. How to get node.warn (x) value as an msg.payload?

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [6 May 2020 03:28 UTC](https://discourse.nodered.org/t/loop-increment-and-decrement/25161/10 "2020-05-06T03:28:02Z")

</div>

msg.payload = x;  
node.send(msg);

---

<div class="post-metadata">

**Author:** ![mrizkywiguna](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrizkywiguna/32/32016_2.png) [@mrizkywiguna](https://discourse.nodered.org/u/mrizkywiguna)\
**Post date:** [6 May 2020 05:01 UTC](https://discourse.nodered.org/t/loop-increment-and-decrement/25161/11 "2020-05-06T05:01:25Z")

</div>

thanks man!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [5 July 2020 05:01 UTC](https://discourse.nodered.org/t/loop-increment-and-decrement/25161/12 "2020-07-05T05:01:26Z")

</div>

Closed on 5 July 2020 05:01 UTC

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
