I have the following function node that takes source11,a global variable, splits the numbers out, matches them to their respective IR commands for each number and then sends that on. I create a global variable called matchedCommands11. Right now my source11 global variable is set to 111; however, my matchedCommands11 global variable is set to the IR commands for 22. My function node code below is not updating the matchedCommands11 variable with the correct value in this case it should be array(0:)1 array(1:) 1 and array(2:) 1. Can anyone tell me what I'm doing wrong?
matchedCommands11 array[2]
0: "1,58000,1,37,23,351,23,164,23,164,23,164,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,164,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351"
1: "1,58000,1,37,23,351,23,164,23,164,23,164,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,164,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351"
[{"id":"8e9589381134810a","type":"function","z":"bfa8244b190d540b","name":"Split digits/Assign IR","func":"// Define IR commands array\nvar irCommands = [\n // ir command 0\n \"1,58000,1,37,23,351,23,164,23,94,23,164,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,94,23,164,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351\",\n // ir command 1\n \"1,58000,1,37,23,351,23,164,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351\",\n // ir command 2\n \"1,58000,1,37,23,351,23,164,23,164,23,164,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,164,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351\",\n // ir command 3\n \"1,58000,1,37,23,351,23,164,23,164,23,164,23,94,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,164,23,94,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351\",\n // ir command 4\n \"1,58000,1,37,23,351,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351\",\n // ir command 5\n \"1,58000,1,37,23,351,23,164,23,164,23,94,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,94,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351\",\n // ir command 6\n \"1,58000,1,37,23,351,23,164,23,164,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351\",\n // ir command 7\n \"1,58000,1,37,23,351,23,164,23,164,23,94,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,94,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351\",\n // ir command 8\n \"1,58000,1,37,23,351,23,164,23,164,23,94,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,94,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351\",\n // ir command 9\n \"1,58000,1,37,23,351,23,164,23,164,23,94,23,94,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,94,23,94,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351\"\n // Add more commands as needed\n];\n\n// Get the value of source11 from the message payload\nvar source11 = msg.payload;\n\nnode.warn('Source11 value: ' + source11);\n\n// Check if source11 is a number\nif (typeof source11 === 'number') {\n // Convert source11 to a string to handle multi-digit numbers\n var source11String = source11.toString();\n\n node.warn('Source11 String: ' + source11String);\n\n // Convert source11String to an array of digits\n var numberArray = source11String.split('').map(Number);\n\n node.warn('Number Array: ' + numberArray);\n\n // Initialize an array to store matched IR commands\n var matchedCommands11 = [];\n\n // Iterate over each digit in the number array\n numberArray.forEach(function(digit) {\n // Ensure the digit is within the range 0-9\n if (digit >= 0 && digit <= 9) {\n // Get the corresponding IR command for the digit\n var command = irCommands[digit];\n // Push the command to the array of matched commands\n matchedCommands11.push(command);\n } else {\n node.warn(\"Invalid digit: \" + digit);\n }\n });\n\n node.warn('Matched Commands: ' + JSON.stringify(matchedCommands11));\n\n // Store the array of matched commands in the global context\n global.set(\"matchedCommands11\", matchedCommands11);\n} else {\n // Handle the case when source11 is not a number\n node.error(\"Default channel must be a number.\");\n}\n\n// Return the message\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2200,"y":880,"wires":[["5dde29537c64f323"]]}]