I'm slowly learning Node Red and its workings, trying to get a function node working with nested if-then-else statements but despite messing for some hours I just can't grasp it. Could someone kindly have a look at my code below and maybe mould it into a working function.
The idea is that if the msg contains an "on" command and the requested RGB colours are as listed it will set the msg object as shown.
If the "on" command is there and the RGB colours requested do not match the figures shown then set the msg as shown.
If the command is "off" then just do that bit.
Sounds so simple but my coding education stopped at visual basic so I'm way behind and getting on in age now sadly.
A cup of coffee helped me out, I was over-complicating things it seems, the working version I now have is below. I guess the lesson learnt is to KISS (keep it simple, stupid)
It is best not to use var. Use let or const instead. Google if you don't know why.
In case you did not realise, instead of if (msg.on === true) you can just use if (msg.on). If you know that msg.on is true or false then the two statements are identical. If msg.on is, for example, 1 or "1" or in fact any string or any non-zero number, then the test will fail pass in the first test, but pass fail in the second.
Edit: corrected above. Non zero numbers and all strings (including "false" and "0") are considered truthy in javascript. Number 0 is considered falsey.
Personally I would not do that. I have seen, many times, programmers updating code that has early return statements add code not noticing that a return had been made further up, meaning that the added code does not get run.