# Back and forward in an array

**URL:** <https://discourse.nodered.org/t/back-and-forward-in-an-array/60659>\
**Category:** General\
**Created:** [1 April 2022 19:55 UTC](https://discourse.nodered.org/t/back-and-forward-in-an-array/60659 "2022-04-01T19:55:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SmartHomeNewbie](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smarthomenewbie/32/35027_2.png) [@SmartHomeNewbie](https://discourse.nodered.org/u/SmartHomeNewbie)\
**Post date:** [1 April 2022 19:55 UTC](https://discourse.nodered.org/t/back-and-forward-in-an-array/60659/1 "2022-04-01T19:55:51Z")

</div>

Hello,

I am trying to make a flow to go back and forward in an array of colors, and for the most part it works  
But when I go from pressing "forward" to "backward", the first time I press backward it goes forward in the array and then starts going backwards, same happens the other way around, when going from backward to forward.  
Could anyone tell me why this happens and how to fix it ?

 ![nodered](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/e/4e03fbd01e28dbadbe9b7de4f0f7025e68a04cd1.jpeg)

```auto
[{"id":"5062e14b16b0ea34","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"5730495f3e741a0b","type":"function","z":"5062e14b16b0ea34","name":"color backward","func":"var colors = flow.get(\"colors\");\n\nvar x = colors.length - 1;\nvar count = flow.get(\"count\");\nvar y = isNaN(count);\n\nif(y === true ){\n count = 0;\n flow.set(\"count\", count);\n}\n\nvar currentColor = colors[count];\ncount--;\n\n\nif(count < 0){\n count = x;\n}\n\nflow.set(\"count\", count);\nmsg.payload = {\"color\":{\"rgb\": currentColor}};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":640,"y":340,"wires":[["c625da7696b47a08"]]},{"id":"5908ca63a8aa9687","type":"function","z":"5062e14b16b0ea34","name":"color forward","func":"var colors = flow.get(\"colors\");\n\nvar x = colors.length - 1;\n\nvar count = flow.get(\"count\");\nvar y = isNaN(count);\n\nif(y === true ){\n count = 0;\n flow.set(\"count\", count);\n}\nvar currentColor = colors[count];\ncount++;\n\n\nif(count > x){\n count = 0;\n}\nflow.set(\"count\", count);\nmsg.payload = {\"color\":{\"rgb\": currentColor}};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":260,"wires":[["c625da7696b47a08"]]},{"id":"c625da7696b47a08","type":"debug","z":"5062e14b16b0ea34","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":990,"y":300,"wires":[]},{"id":"c6aa9299edd98641","type":"inject","z":"5062e14b16b0ea34","name":"color array","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"one\",\"two\",\"three\"]","payloadType":"json","x":420,"y":180,"wires":[["3004e429aad190ac"]]},{"id":"3004e429aad190ac","type":"function","z":"5062e14b16b0ea34","name":"Set colors","func":"var x = msg.payload;\nflow.set(\"colors\", x);\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":640,"y":180,"wires":[[]]},{"id":"d9c32ef22a7782a6","type":"inject","z":"5062e14b16b0ea34","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":420,"y":260,"wires":[["5908ca63a8aa9687"]]},{"id":"3d5ddee51f141f3b","type":"inject","z":"5062e14b16b0ea34","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":420,"y":340,"wires":[["5730495f3e741a0b"]]}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 April 2022 20:02 UTC](https://discourse.nodered.org/t/back-and-forward-in-an-array/60659/2 "2022-04-01T20:02:00Z")

</div>

Like this?  
 ![chrome_08YFsP4yKl](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/a/ba3a66ecd0d78ab1491de439999be3d3c16d26e3.gif)

```auto
[
    {
        "id": "5730495f3e741a0b",
        "type": "function",
        "z": "5062e14b16b0ea34",
        "name": "color backward",
        "func": "const colors = flow.get(\"colors\");\nconst colorMaxIdx = colors.length - 1;\nlet pos = flow.get(\"pos\") || 0;\nlet currentColor = colors[pos];\nif (pos <= 0) {\n pos = colorMaxIdx;\n} else {\n pos--;\n}\n\ncurrentColor = colors[pos];\n\nflow.set(\"pos\", pos);\nmsg.payload = { \"color\": { \"rgb\": currentColor } };\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 640,
        "y": 340,
        "wires": [
            [
                "c625da7696b47a08"
            ]
        ]
    },
    {
        "id": "5908ca63a8aa9687",
        "type": "function",
        "z": "5062e14b16b0ea34",
        "name": "color forward",
        "func": "const colors = flow.get(\"colors\");\nconst colorMaxIdx = colors.length - 1;\nlet pos = flow.get(\"pos\") || 0;\nlet currentColor = colors[pos];\nif (pos >= colorMaxIdx) {\n pos = 0;\n} else {\n pos++;\n}\n\ncurrentColor = colors[pos];\n\nflow.set(\"pos\", pos);\nmsg.payload = {\"color\":{\"rgb\": currentColor}};\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 630,
        "y": 260,
        "wires": [
            [
                "c625da7696b47a08"
            ]
        ]
    },
    {
        "id": "c625da7696b47a08",
        "type": "debug",
        "z": "5062e14b16b0ea34",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 990,
        "y": 300,
        "wires": []
    },
    {
        "id": "c6aa9299edd98641",
        "type": "inject",
        "z": "5062e14b16b0ea34",
        "name": "color array",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "[\"one\",\"two\",\"three\"]",
        "payloadType": "json",
        "x": 420,
        "y": 180,
        "wires": [
            [
                "3004e429aad190ac"
            ]
        ]
    },
    {
        "id": "3004e429aad190ac",
        "type": "function",
        "z": "5062e14b16b0ea34",
        "name": "Set colors",
        "func": "var x = msg.payload;\nflow.set(\"colors\", x);\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 640,
        "y": 180,
        "wires": [
            []
        ]
    },
    {
        "id": "d9c32ef22a7782a6",
        "type": "inject",
        "z": "5062e14b16b0ea34",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 420,
        "y": 260,
        "wires": [
            [
                "5908ca63a8aa9687"
            ]
        ]
    },
    {
        "id": "3d5ddee51f141f3b",
        "type": "inject",
        "z": "5062e14b16b0ea34",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 420,
        "y": 340,
        "wires": [
            [
                "5730495f3e741a0b"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![SmartHomeNewbie](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smarthomenewbie/32/35027_2.png) [@SmartHomeNewbie](https://discourse.nodered.org/u/SmartHomeNewbie)\
**Post date:** [1 April 2022 21:22 UTC](https://discourse.nodered.org/t/back-and-forward-in-an-array/60659/3 "2022-04-01T21:22:01Z")

</div>

Exactly like that, thanks a lot for the super fast reply!  
I'm trying to understand why my example didn't work, is this the reason? or is there more to it ?

 ![var](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/5/459be2d07d3a861f5a3dc6b741bdbfcd5368def1.jpeg)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 April 2022 21:28 UTC](https://discourse.nodered.org/t/back-and-forward-in-an-array/60659/4 "2022-04-01T21:28:22Z")

</div>

No, usage of `var` was not your issue here.

In your case, you were collecting the color value BEFORE adjusting the index position

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/3/1338fdd28f47cd4c1ea264831a951d89ff2c196d.png)

---

<div class="post-metadata">

**Author:** ![SmartHomeNewbie](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smarthomenewbie/32/35027_2.png) [@SmartHomeNewbie](https://discourse.nodered.org/u/SmartHomeNewbie)\
**Post date:** [1 April 2022 21:35 UTC](https://discourse.nodered.org/t/back-and-forward-in-an-array/60659/5 "2022-04-01T21:35:53Z")

</div>

Ofcouse, Thanks again for the help, node status in the debug node was a nice trick too 🙂

---

<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:** [15 April 2022 21:36 UTC](https://discourse.nodered.org/t/back-and-forward-in-an-array/60659/6 "2022-04-15T21:36:23Z")

</div>

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