• Global community
• Language:
• Deutsch
• English
• Español
• Français
• Português
• 日本語コミュニティ
Dedicated community for Japanese speakers
• 한국 커뮤니티
Dedicated community for Korean speakers
Exit
• Script to find the minimum of 4 fields EXCLUDING a...

# 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

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

449

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

6 Replies 6
Community Expert ,
Jan 14, 2019 Jan 14, 2019

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

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

Copied

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

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

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

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

Copied

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

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

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,

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

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.