Help with JS transformation

Im new to JS.
I want to get contact status from 10th digit from that line “11111010000001101011100000000000101100” (1 or 0)

Contact Contact_Noo_Test { mqtt="<[wb:/events/wb-homa-rcd/protocols/noo:state:JS(window.js)]" }

and my script (not work):

(function(w){ var str=new String(w) w=str.substring(9, 10); } return w; }) (input)
Can anybody help with it?

Maybe something like:

(function(w,start,end) {
  return w.substring(start, end) === "0" ? "CLOSED" : "OPEN";

Wow, works perfect!
Many thanks, watou.

One more (stupid) question - if in input line i have a mark “qwr”, how to return value only when this mark is present?

(function(w,start,end,mark) {
  return w.substring(start, end) === "0" ? "CLOSED" : "OPEN";

Well, the transform has to return some value. If you have a Contact item, what state do you want it to be in if the string “qwr” isn’t present in the input string? The change you made above is missing a parenthesis, for one thing. I don’t know if this will work the way you want it to, because I’m not sure of what you want. But please study this variation:

(function(w,start,end) {
  if (w.indexOf("qwr") > -1) {
    return w.substring(start, end) === "0" ? "CLOSED" : "OPEN";
  } else {
    return ""; // no idea if this is what you want

Please read up on JavaScript programming from any of a number of helpful web sites. I think, once you’ve gotten proficiency there, you can carry on solo! All the best!

You are the best :+1:

1 Like