if (x == 0)
{
// do something here;
}
// continue as normal.
But say I want to check x is..... a number? (and is between 0 and 8) (ok, that's just another if nested in there)
How do I do that?
(Check it is a number?)
I know in the switch node there are options of type.....
But I don't know the finer details of how that works.
So I'm asking here, now.
// Split the payload.
let check = msg.payload.split(",");
//node.warn(check);
let error = 0;
let a = "";
if (error == 0)
{
a = parseInt(check[1]);
node.warn("A " + a);
if ("number" === typeof a)
{
// Part 1 problem.
node.warn("Part 1 problem.");
error = 1;
}
}
Given I am sending it rgb,0,0,0,0
Why do I get an error?
Not from what you showed me. It just likes to spin.
There are 4 parts to parse/scan.
0 has to be rgb, - which is easy to test.
1 - has to be a NUMBER from 0 to 8. The code has to catch if NOT meeting that.
(Sorry, my bad. (maybe)..... But I had posted my effort and that is how the main function node works.)
2 - as 1, but form 0 to 255
3 and 4 like 2.
After your let a = parseInt(check[1]); a will either be a valid number or else NaN
But there is no value in testing if (typeof a === "number") because typeof NaN is still "number"
What you can do is test if a is between 0 and 256:
// Split the payload.
let check = msg.payload.split(",");
//node.warn(check);
let a = parseInt(check[1]);
let type = typeof a
node.warn(`typeof ${a} is ${type}`);
if (a >= 0 && a<256) {
node.warn (`${a} is a valid number`)
}
else {
node.warn (`${a} is not a valid number`)
}
return msg
The point I am trying to make is that let a = parseInt(check[1])forces typeof a to be "number" even if it is not a valid number (NaN)
At least that's how I understand it.