Sergey Belov
Sergey Belov

Reputation: 59

Why isn't if statement executed

There is a task to count the number of words "not_available", if 12 then send 1, if at least one of them is "available" send 0

Below is a sample code that still sends 0 even if the number of "not_available" = 12

What was I wrong about?

var Count_available = (rootObj.match(/AVAILABLE/g) || []).length
var Count_unavailable = (rootObj.match(/NOT_AVAILABLE/g) || []).length

if (Count_available >= 1) {
  var jsondata = {
    "type": "SberLogistic.Availability",
    "series": [{
      "timeseriesId": "custom:syntheticmonitors.availability",
      "dimensions": {
        "config_id": "CI02874375"
      },
      "dataPoints": [
        [Number(new Date()), 1]
      ] //send 1

    }]
  }
} else if (Count_unavailable == 12) {
  var jsondata = {
    "type": "SberLogistic.Availability",
    "series": [{
      "timeseriesId": "custom:syntheticmonitors.availability",
      "dimensions": {
        "config_id": "CI02874375"
      },
      "dataPoints": [
        [Number(new Date()), 0]
      ] //send 0

    }]
  }
};

Upvotes: 2

Views: 98

Answers (1)

Timothy Alexis Vass
Timothy Alexis Vass

Reputation: 2715

If at least 1 is available, it will return without checking the else if.

Reverse the order, then it will check unavailable before available.

    if (Count_unavailable == 12) {
    ...
    } else if (Count_available >= 1) {
    ...
    };

Upvotes: 3

Related Questions