Hello,
I am in pop up configuration, and my expression was working properly but now, it is not, instead showing N/A
below is my code:
function safeRound(val) {
if (IsEmpty(val)) { return null; }
var n = Number(val);
if (IsEmpty(n)) { return null; }
return Round(n);
}
function ordinal(n) {
var d = n % 100;
if (d >= 11 && d <= 13) {
return Text(n) + "th";
}
var r = n % 10;
if (r == 1) {
return Text(n) + "st";
}
if (r == 2) {
return Text(n) + "nd";
}
if (r == 3) {
return Text(n) + "rd";
}
return Text(n) + "th";
}
// simple title case: capitalizes first letter of each word
function titleCase(s) {
var words = Split(s, " ");
var out = [];
for (var i in words) {
var w = words[i];
if (Count(w) == 0) {
Push(out, w);
continue;
}
var hyphens = Split(w, "-");
var rebuilt = [];
for (var j in hyphens) {
var piece = hyphens[j];
var first = Upper(Left(piece, 1));
var rest = "";
if (Count(piece) > 1) {
rest = Lower(Right(piece, Count(piece) - 1));
}
Push(rebuilt, first + rest);
}
Push(out, Concatenate(rebuilt, "-"));
}
return Concatenate(out, " ");
}
// safe estimated population fetch; expects fields named like <BASE>_EstPop
function getEstPop(base) {
var fieldName = base + "_EstPop";
var raw = DefaultValue($feature, fieldName, "N/A");
if (raw == "N/A" || IsEmpty(raw)) {
return "N/A";
}
var num = Number(raw);
if (IsEmpty(num)) {
return "N/A";
}
return Text(Round(num));
}
// selection-sort descending without Remove
function sortDesc(items) {
var out = [];
var working = items;
while (Count(working) > 0) {
var maxIdx = 0;
for (var i = 1; i < Count(working); i++) {
if (working[i].score > working[maxIdx].score) {
maxIdx = i;
}
}
Push(out, working[maxIdx]);
var next = [];
for (var j = 0; j < Count(working); j++) {
if (j != maxIdx) {
Push(next, working[j]);
}
}
working = next;
}
return out;
}
// buckets as arrays of objects
var veryHigh = [];
var high = [];
var medium = [];
//Health Outcomes
// TEETHLOST
var t1 = safeRound($feature['CDC_PLACES_2175.TEETHLOST_percentile']);
var est1 = getEstPop("TEETHLOST");
if (t1 != null) {
if (t1 >= 90) { Push(veryHigh, { label: "All teeth lost", score: t1, estPop: est1 }); }
else if (t1 >= 80) { Push(high, { label: "All teeth lost", score: t1, estPop: est1 }); }
else if (t1 >= 70) { Push(medium, { label: "All teeth lost", score: t1, estPop: est1 }); }
}
// ARTHRITIS
var t3 = safeRound($feature['CDC_PLACES_2175.ARTHRITIS_percentile']);
var est3 = getEstPop("ARTHRITIS");
if (t3 != null) {
if (t3 >= 90) { Push(veryHigh, { label: "Arthritis", score: t3, estPop: est3 }); }
else if (t3 >= 80) { Push(high, { label: "Arthritis", score: t3, estPop: est3 }); }
else if (t3 >= 70) { Push(medium, { label: "Arthritis", score: t3, estPop: est3 }); }
}
// CANCER
var t6 = safeRound($feature['CDC_PLACES_2175.CANCER_percentile']);
var est6 = getEstPop("CANCER");
if (t6 != null) {
if (t6 >= 90) { Push(veryHigh, { label: "Cancer (non-skin) or melanoma", score: t6, estPop: est6 }); }
else if (t6 >= 80) { Push(high, { label: "Cancer (non-skin) or melanoma", score: t6, estPop: est6 }); }
else if (t6 >= 70) { Push(medium, { label: "Cancer (non-skin) or melanoma", score: t6, estPop: est6 }); }
}
// COPD
var t10 = safeRound($feature['CDC_PLACES_2175.COPD_percentile']);
var est10 = getEstPop("COPD");
if (t10 != null) {
if (t10 >= 90) { Push(veryHigh, { label: "COPD", score: t10, estPop: est10 }); }
else if (t10 >= 80) { Push(high, { label: "COPD", score: t10, estPop: est10 }); }
else if (t10 >= 70) { Push(medium, { label: "COPD", score: t10, estPop: est10 }); }
}
// CHD
var t11 = safeRound($feature['CDC_PLACES_2175.CHD_percentile']);
var est11 = getEstPop("CHD");
if (t11 != null) {
if (t11 >= 90) { Push(veryHigh, { label: "Coronary heart disease", score: t11, estPop: est11 }); }
else if (t11 >= 80) { Push(high, { label: "Coronary heart disease", score: t11, estPop: est11 }); }
else if (t11 >= 70) { Push(medium, { label: "Coronary heart disease", score: t11, estPop: est11 }); }
}
// CASTHMA
var t12 = safeRound($feature['CDC_PLACES_2175.CASTHMA_percentile']);
var est12 = getEstPop("CASTHMA");
if (t12 != null) {
if (t12 >= 90) { Push(veryHigh, { label: "Current asthma", score: t12, estPop: est12 }); }
else if (t12 >= 80) { Push(high, { label: "Current asthma", score: t12, estPop: est12 }); }
else if (t12 >= 70) { Push(medium, { label: "Current asthma", score: t12, estPop: est12 }); }
}
// DEPRESSION
var t15 = safeRound($feature['CDC_PLACES_2175.DEPRESSION_percentile']);
var est15 = getEstPop("DEPRESSION");
if (t15 != null) {
if (t15 >= 90) { Push(veryHigh, { label: "Depression", score: t15, estPop: est15 }); }
else if (t15 >= 80) { Push(high, { label: "Depression", score: t15, estPop: est15 }); }
else if (t15 >= 70) { Push(medium, { label: "Depression", score: t15, estPop: est15 }); }
}
// DIABETES
var t16 = safeRound($feature['CDC_PLACES_2175.DIABETES_percentile']);
var est16 = getEstPop("DIABETES");
if (t16 != null) {
if (t16 >= 90) { Push(veryHigh, { label: "Diabetes", score: t16, estPop: est16 }); }
else if (t16 >= 80) { Push(high, { label: "Diabetes", score: t16, estPop: est16 }); }
else if (t16 >= 70) { Push(medium, { label: "Diabetes", score: t16, estPop: est16 }); }
}
// BPHIGH
var t22 = safeRound($feature['CDC_PLACES_2175.BPHIGH_percentile']);
var est22 = getEstPop("BPHIGH");
if (t22 != null) {
if (t22 >= 90) { Push(veryHigh, { label: "High blood pressure", score: t22, estPop: est22 }); }
else if (t22 >= 80) { Push(high, { label: "High blood pressure", score: t22, estPop: est22 }); }
else if (t22 >= 70) { Push(medium, { label: "High blood pressure", score: t22, estPop: est22 }); }
}
// HIGHCHOL
var t23 = safeRound($feature['CDC_PLACES_2175.HIGHCHOL_percentile']);
var est23 = getEstPop("HIGHCHOL");
if (t23 != null) {
if (t23 >= 90) { Push(veryHigh, { label: "High cholesterol", score: t23, estPop: est23 }); }
else if (t23 >= 80) { Push(high, { label: "High cholesterol", score: t23, estPop: est23 }); }
else if (t23 >= 70) { Push(medium, { label: "High cholesterol", score: t23, estPop: est23 }); }
}
// OBESITY
var t30 = safeRound($feature['CDC_PLACES_2175.OBESITY_percentile']);
var est30 = getEstPop("OBESITY");
if (t30 != null) {
if (t30 >= 90) { Push(veryHigh, { label: "Obesity", score: t30, estPop: est30 }); }
else if (t30 >= 80) { Push(high, { label: "Obesity", score: t30, estPop: est30 }); }
else if (t30 >= 70) { Push(medium, { label: "Obesity", score: t30, estPop: est30 }); }
}
// STROKE
var t37 = safeRound($feature['CDC_PLACES_2175.STROKE_percentile']);
var est37 = getEstPop("STROKE");
if (t37 != null) {
if (t37 >= 90) { Push(veryHigh, { label: "Stroke", score: t37, estPop: est37 }); }
else if (t37 >= 80) { Push(high, { label: "Stroke", score: t37, estPop: est37 }); }
else if (t37 >= 70) { Push(medium, { label: "Stroke", score: t37, estPop: est37 }); }
}
// sort buckets descending
veryHigh = sortDesc(veryHigh);
high = sortDesc(high);
medium = sortDesc(medium);
// format back to strings with title case applied to labels, including percentile and est pop
function format(arr) {
var out = [];
for (var i in arr) {
var o = arr[i];
var pct = ordinal(o.score);
var estText = When(o.estPop == "N/A", "N/A", o.estPop);
Push(out, titleCase(o.label) + " (" + pct + " Percentile and Est Pop: " + estText + ")");
}
return out;
}
var vhStr = format(veryHigh);
var hStr = format(high);
var mStr = format(medium);
// assemble output with HTML breaks between categories
var parts = [];
if (Count(vhStr) > 0) {
Push(parts, "<b>Very High (" + Count(vhStr) + "):</b> " + Concatenate(vhStr, ", "));
}
if (Count(hStr) > 0) {
Push(parts, "<b>High (" + Count(hStr) + "):</b> " + Concatenate(hStr, ", "));
}
if (Count(mStr) > 0) {
Push(parts, "<b>Medium (" + Count(mStr) + "):</b> " + Concatenate(mStr, ", "));
}
if (Count(parts) == 0) {
return "No conditions at Medium or above";
}
return Concatenate(parts, "<br><br>");