Back and forward in an array

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 ?

[{"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"]]}]

Like this?
chrome_08YFsP4yKl

[
    {
        "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"
            ]
        ]
    }
]
1 Like

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 ?

No, usage of var was not your issue here.

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

image

1 Like

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

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