Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Javascript Math.min calculations

Community Beginner ,
Sep 22, 2017 Sep 22, 2017

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.

TOPICS
PDF forms
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 22, 2017 Sep 22, 2017

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);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 22, 2017 Sep 22, 2017

Okay, thank you. I will give this a try.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 22, 2017 Sep 22, 2017
LATEST

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;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines