Javascript Math.min calculations
Copy link to clipboard
Copied
My calculation will pull the lowest number if all the fields are filled in. When there is an empty field it automatically set the lowest number as zero. How do I get the calculation to read the fields that have number entered in them and ignore the empty fields.
Copy link to clipboard
Copied
It's problematic to use Math.min for that. I would create an array of all non-empty values and then use a simple function to find the lowest value in it. Something like this:
var a = this.getField("A").valueAsString;
var b = this.getField("B").valueAsString;
var c = this.getField("C").valueAsString;
var nonEmptyValues = [];
if (a!="") nonEmptyValues.push(Number(a));
if (b!="") nonEmptyValues.push(Number(b));
if (c!="") nonEmptyValues.push(Number(c));
if (nonEmptyValues.length==0) event.value = "";
else {
nonEmptyValues.sort(numericSort);
event.value = nonEmptyValues[0];
}
function numericSort(a,b) {
return Number(a) - Number(b);
}
Copy link to clipboard
Copied
Okay, thank you. I will give this a try.
Copy link to clipboard
Copied
This is what I currently have been using and for the most part it has worked uptuntil now. Comps 3 has no number entered in the field.
var verylargenum = 999999;
var comps;
if (this.getField("CRow1").valueAsString === "")
{
comps = verylargenum;
} else
{
comps = this.getField("CRow1").value;
}
var comps1;
if (this.getField("CRow2").valueAsString === "")
{
comps1 = verylargenum;
} else
{
comps1 = this.getField("CRow2").value;
}
var comps2;
if (this.getField("CRow3").valueAsString === "")
{
comps2 = verylargenum;
} else
{
comps2 = this.getField("CRow3").value;
}
var comps3;
if (this.getField("CRow4").valueAsString === "")
{
comps3 = verylargenum;
} else
{
comps3 = this.getField("CRow4").value;
}
var smallest = Math.min(comps,comps1,comps2,comps3);
if (smallest == verylargenum)
{
event.value = "";
} else
{
event.value = smallest;
}

