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

Script to find the minimum of 4 fields EXCLUDING a field if it has zeros in it

Community Beginner ,
Jan 14, 2019 Jan 14, 2019

Copy link to clipboard

Copied

var aNumFields = ["FieldA", "FieldB", "FieldC", "FieldD"];

function Min("aNumFields")
{
    // n = number of fields that have a numerical value > 0
    var i = 0;
    for ( var i = 0; i<aNumFields.length; i++) {
       var v = this.getField(aNumFields).value;
           if ( v !=  ) {
             n++;
             min = v;
           }
    }
}
event.value = minValue;

I have changed this script around so many times I have probably made it impossible to correct, any assistance you can give would be greatly appreciated.

TOPICS
Acrobat SDK and JavaScript , Windows

Views

144

Likes

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , Jan 14, 2019 Jan 14, 2019

Use this code as the custom calculation script of the field:

function Min(aFields) {

    var min = Infinity;

    for (var i = 0; i<aFields.length; i++) {

        var v = Number(this.getField(aFields).value);

        if (v!=0 && v<min) min = v;

    }

    return (min==Infinity) ? "" : min;

}

var aFields = ["FieldA", "FieldB", "FieldC", "FieldD"];

event.value = Min(aFields);

Likes

Translate

Translate
Community Expert ,
Jan 14, 2019 Jan 14, 2019

Copy link to clipboard

Copied

Yeah, that code is a mess... I can fix it for you but I need to know what should it return if all the fields are empty (or zero)?

Likes

Translate

Translate

Report

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 ,
Jan 14, 2019 Jan 14, 2019

Copy link to clipboard

Copied

The Text box should stay blank if all the fields are zeros.

Likes

Translate

Translate

Report

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 ,
Jan 14, 2019 Jan 14, 2019

Copy link to clipboard

Copied

Use this code as the custom calculation script of the field:

function Min(aFields) {

    var min = Infinity;

    for (var i = 0; i<aFields.length; i++) {

        var v = Number(this.getField(aFields).value);

        if (v!=0 && v<min) min = v;

    }

    return (min==Infinity) ? "" : min;

}

var aFields = ["FieldA", "FieldB", "FieldC", "FieldD"];

event.value = Min(aFields);

Likes

Translate

Translate

Report

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 ,
Jan 14, 2019 Jan 14, 2019

Copy link to clipboard

Copied

Thank you I saved it and it worked like a charm.

Likes

Translate

Translate

Report

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 ,
Jan 14, 2019 Jan 14, 2019

Copy link to clipboard

Copied

Thank you so very much.

I would have never thought to put the var fields and value at
the end.

I must have changed it 100 times, I think it is back to school
for me.  Using Java once in a blue moon I sure did forget a lot.

Thank you,

Likes

Translate

Translate

Report

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 ,
Jan 14, 2019 Jan 14, 2019

Copy link to clipboard

Copied

LATEST

The location of it doesn't matter, actually. Could be before or after the function definition. The main issue was the code inside the function.

And if you want to learn it the first thing to realize is it's JavaScript, not Java. The names are similar, but the languages are quite different.

Likes

Translate

Translate

Report

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