Skip to main content
Participating Frequently
January 14, 2019
Answered

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

  • January 14, 2019
  • 3 replies
  • 677 views

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.

This topic has been closed for replies.
Correct answer try67

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

3 replies

Participating Frequently
January 14, 2019

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,

try67
Community Expert
Community Expert
January 14, 2019

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.

Participating Frequently
January 14, 2019

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

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
January 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);

Participating Frequently
January 14, 2019

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

try67
Community Expert
Community Expert
January 14, 2019

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