Skip to main content
Known Participant
December 12, 2017
Question

Can a boolean be set up to find the max value?

  • December 12, 2017
  • 5 replies
  • 2176 views

/*

          @17537118

                aFields : an array of the field names to use for the calculation

                bIgnoreBlanks : a boolean that tells the function whether or not to ignore blank fields

                bIgnoreZeros : a boolean that tells the function whether or not to ignore zero values

        */ 

        function calcMax(fields, ignoreBlanks, ignoreZeros) { 

            var total = 0; 

            var n = 0;

         for (var i in fields) {

             var f = this.getField(fields); 

             if (f==null) { 

                 console.println("Error! Can't locate a field called: " + fields); 

                 continue; 

             } 

             if (f.valueAsString=="" && ignoreBlanks) continue; 

             var v = Number(f.valueAsString); 

             if (isNaN(v)) continue; 

             if (v==0 && ignoreZeros) continue; 

             total+=v; 

             n++; 

         } 

         if (n==0) event.value = ""; 

         else event.value = total; 

     }

calcMax(["DT1Moisture Content 01 pcf","DT1Moisture Content 01 pcf_2"], true,true);

I have tried to make this one work, but I am not getting the correct answer.

This topic has been closed for replies.

5 replies

Known Participant
December 19, 2017

Nothing, the filed remains blank.

try67
Community Expert
Community Expert
December 19, 2017

Press Ctrl+J and check if there are any error messages.

Also, you have to change the value of one of the fields in the file for it to "kick in".

Known Participant
December 19, 2017

I gave the formula a try and it is still not working, any suggestions?

Bernd Alheit
Community Expert
Community Expert
December 19, 2017

What happens when you use the function?

Known Participant
December 12, 2017

I would like for the code to go through all the fields and select the max out of all the answers. And right now it is totaling them. I was able to change a few things to make it find the average of a list of numbers, but cant seem to figure out how to get it to find the max.

try67
Community Expert
Community Expert
December 12, 2017

This code was originally written to calculate an average value. I see that you changed it, but you should not edit code unless you know what you're doing...

I've made the adjustments for you. Try this version:

/*

  @params

        aFields : an array of the field names to use for the calculation

        bIgnoreBlanks : a boolean that tells the function whether or not to ignore blank fields

        bIgnoreZeros : a boolean that tells the function whether or not to ignore zero values

*/

function calcMax(fields, ignoreBlanks, ignoreZeros) {

    var max = -Infinity;

    for (var i in fields) {

        var f = this.getField(fields);

        if (f==null) {

            console.println("Error! Can't locate a field called: " + fields);

            continue;

         }

         if (f.valueAsString=="" && ignoreBlanks) continue;

         var v = Number(f.valueAsString);

         if (isNaN(v)) continue;

         if (v==0 && ignoreZeros) continue;

         if (v>max) max = v;

     }

     if (max==-Infinity) event.value = "";

     else event.value = max;

}

Bernd Alheit
Community Expert
Community Expert
December 12, 2017

This calculates the sum of the fields. What want you?

try67
Community Expert
Community Expert
December 12, 2017

Since I wrote this code, I might be able to help... What are you getting? What are the results?

try67
Community Expert
Community Expert
December 12, 2017

Also, what does a boolean value have to do with it?