How to create an array with flow variables

I am developing a function node where I perform operations with flow variables, as you can see in the image, each operation performed depends on a flow variable saved a couple of lines before.

The first problem is that after some operations, the values that I am supposed to return are not correct or "NaN" is displayed in the flow variable of said operation.

The second is that I need all those results (39 results) to be in a single array, I tried but I am new to javascript and I have syntax errors.

I hope I have explained well, thanks for helping me.

Flow:

[{"id":"c9793b5a.9b7648","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"808affa4.34e94","type":"function","z":"c9793b5a.9b7648","name":"Datos para caracteristica nominal","func":"var ER = 0;\nvar EX = 0;\nvar Diam = (6.1732*2);\nvar Rad = (Diam/2);\nflow.set (\"Diam\" , Diam);\nvar IntR = Math.abs(((ER - Rad) - (ER + Rad))/8);\nflow.set (\"IntR\" , IntR);\n\nvar CN_R1 = (ER - Rad);\nflow.set(\"CN_R1\", CN_R1);\n\nvar CN_R2 = (flow.get(\"CN_R1\") + 0.2 * IntR) ;\nflow.set(\"CN_R2\", CN_R2);\n \nvar CN_R3 = (flow.get(\"CN_R2\") + 0.4 * IntR);\nflow.set(\"CN_R3\", CN_R3);\n\nvar CN_R4 = (flow.get(\"CN_R3\") + 0.6 * IntR);\nflow.set(\"CN_R4\", CN_R4);\n\nvar CN_R5 = (flow.get(\"CN_R4\") + 0.8 * IntR);\nflow.set(\"CN_R5\", CN_R5);\n\nvar CN_R6 = (flow.get(\"CN_R5\") + 1 * IntR);\nflow.set(\"CN_R6\", CN_R6);\n\nvar CN_R7 = (flow.get(\"CN_R6\") + 1.5 * IntR);\nflow.set(\"CN_R7\", CN_R7);\n\nvar CN_R8 = (flow.get(\"CN_R7\") + 2 * IntR);\nflow.set(\"CN_R8\", CN_R8);\n\nvar CN_R9 = (flow.get(\"CN_R8\") + 2.5 * IntR);\nflow.set(\"CN_R9\", CN_R9);\n\nvar CN_R10 = (flow.get(\"CN_R9\") + 3 * IntR);\nflow.set(\"CN_R10\", CN_R10);\n\nvar CN_R11 = (flow.get(\"CN_R10\") + 3.5 * IntR);\nflow.set(\"CN_R11\", CN_R11);\n\nvar CN_R12 = (flow.get(\"CN_R11\") + 4 * IntR);\nflow.set(\"CN_R12\", CN_R12);\n\nvar CN_R13 = (flow.get(\"CN_R12\") + 4.5 * IntR);\nflow.set(\"CN_R13\", CN_R13);\n\nvar CN_R14 = (flow.get(\"CN_R13\") + 5 * IntR);\nflow.set(\"CN_R14\", CN_R14);\n\nvar CN_R15 = (flow.get(\"CN_R14\") + 5.5 * IntR);\nflow.set(\"CN_R15\", CN_R15);\n\nvar CN_R16 = (flow.get(\"CN_R15\") + 6 * IntR);\nflow.set(\"CN_R16\", CN_R16);\n\nvar CN_R17 = (flow.get(\"CN_R16\") + 6.5 * IntR);\nflow.set(\"CN_R17\", CN_R17);\n\nvar CN_R18 = (flow.get(\"CN_R17\") + 7 * IntR);\nflow.set(\"CN_R18\", CN_R18);\n\nvar CN_R19 = (flow.get(\"CN_R18\") + 7.5 * IntR);\nflow.set(\"CN_R18\", CN_R18);\n\nvar CN_R20 = (flow.get(\"CN_R19\") + 8 * IntR);\nflow.set(\"CN_R20\", CN_R20);\n\nvar CN_R21 = (flow.get(\"CN_R20\") + 7.5 * IntR);\nflow.set(\"CN_R21\", CN_R21);\n\nvar CN_R22 = (flow.get(\"CN_R21\") + 7 * IntR);\nflow.set(\"CN_R22\", CN_R22);\n\nvar CN_R23 = (flow.get(\"CN_R22\") + 6.5 * IntR);\nflow.set(\"CN_R23\", CN_R23);\n\nvar CN_R24 = (flow.get(\"CN_R23\") + 6 * IntR);\nflow.set(\"CN_R24\", CN_R24);\n\nvar CN_R25 = (flow.get(\"CN_R24\") + 5.5 * IntR);\nflow.set(\"CN_R25\", CN_R25);\n\nvar CN_R26 = (flow.get(\"CN_R25\") + 5 * IntR);\nflow.set(\"CN_R26\", CN_R26);\n\nvar CN_R27 = (flow.get(\"CN_R26\") + 4.5 * IntR);\nflow.set(\"CN_R27\", CN_R27);\n\nvar CN_R28 = (flow.get(\"CN_R27\") + 4 * IntR);\nflow.set(\"CN_R28\", CN_R28);\n\nvar CN_R29 = (flow.get(\"CN_R28\") + 3.5 * IntR);\nflow.set(\"CN_R29\", CN_R29);\n\nvar CN_R30 = (flow.get(\"CN_R29\") + 3 * IntR);\nflow.set(\"CN_R30\", CN_R30);\n\nvar CN_R31 = (flow.get(\"CN_R30\") + 2.5 * IntR);\nflow.set(\"CN_R31\", CN_R31);\n\nvar CN_R32 = (flow.get(\"CN_R31\") + 2 * IntR);\nflow.set(\"CN_R32\", CN_R32);\n\nvar CN_R33 = (flow.get(\"CN_R32\") + 1.5 * IntR);\nflow.set(\"CN_R33\", CN_R33);\n\nvar CN_R34 = (flow.get(\"CN_R33\") + 1 * IntR);\nflow.set(\"CN_R34\", CN_R34);\n\nvar CN_R35 = (flow.get(\"CN_R34\") + 0.8 * IntR);\nflow.set(\"CN_R35\", CN_R35);\n\nvar CN_R36 = (flow.get(\"CN_R35\") + 0.6 * IntR);\nflow.set(\"CN_R36\", CN_R36);\n\nvar CN_R37 = (flow.get(\"CN_R36\") + 0.4 * IntR);\nflow.set(\"CN_R37\", CN_R37);\n\nvar CN_R38 = (flow.get(\"CN_R37\") + 0.2 * IntR);\nflow.set(\"CN_R38\", CN_R38);\n\nvar CN_R39 = (flow.get(\"CN_R38\") + 0 * IntR);\nflow.set(\"CN_R39\", CN_R39);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":960,"y":360,"wires":[["f24c7c29.4d5f8"]]},{"id":"665926fe.8eadc8","type":"inject","z":"c9793b5a.9b7648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":870,"y":300,"wires":[["808affa4.34e94"]]},{"id":"f24c7c29.4d5f8","type":"debug","z":"c9793b5a.9b7648","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1060,"y":420,"wires":[]}]

You will need to learn a bit about how computers represent floating point numbers (numbers with digits after the decimal point). Because they use a limited number of bits, it's not possible to represent most numbers exactly, apart from a small set of specific values. So when for example you get CN_R3 = -5.24721999... rather than -5.24722 it's the best that can be achieved with the limits of the system.

If you want to research the details of how floating point numbers are represented, this Wikipedia article is quite good - Floating-point arithmetic - Wikipedia.

The reason you're getting "NaN" (Not a Number) for CN_R20 onwards is that you have a typo in your function :

var CN_R19 = (flow.get("CN_R18") + 7.5 * IntR);
flow.set("CN_R18", CN_R18); // should be CN_R19

A very easy-to-do copy'n'paste error, which we've all made :slight_smile:

1 Like

Thanks for the help, any advice with the array question?

I don't know if you have experience in other languages, but JavaScript arrays are similar to most others. You can declare one at the start of the function :

var myArray = [ ];

and then either enter data directly by index :

myArray[0] = CN_R1;
myArray[1] = CN_R2; // etc.

or use the "push" operator :

myArray.push(CN_R1);
myArray.push(CN_R2);

If you're new to JavaScript it would be worth checking through a few online tutorials to get to know the essentials.

3 Likes