Skip to main content
rakeshk21205956
Inspiring
July 20, 2016
Answered

pdf Javascript To Find greatest number among the given numbers

  • July 20, 2016
  • 2 replies
  • 5105 views

I have form in which there are 28 Fields in which numbers will be filled in, and in the 29th field greatest number among the 28 number is to be shown.

Like   if the fields are populated with

1

5

6

7

9

5

10

50

68

15

15

60

485

58

68

695

58

94

28

38

76

46

86

248

Then the Field should show the greatest number among these i.e 695

This topic has been closed for replies.
Correct answer Karl Heinz Kremer

Sir,

As per your suggestion i changed the Fields name to DP.0, DP.1 ..... DP.27

below is the script i am trying to execute after incorporating the changes suggested by you,     but it is not printing anything.....

var maxValid = false;

var max = 0;

for (var i=0; i<=28; i++)

{

var val = parseInt(this.getField("DP." + i).value);

if (!isNaN(val)) // we have a valid number

{ if (maxValid == true)

{ // this is the first valid value

max = val;

maxValid = false;

}

else

{

max = Math.max(max, val);

}

}

}

if (maxValid)

event.value = max;

else

event.value = "";


If you have fields named from 0 to 27, you cannot run your loop from 0 to 28 - unless you have an exception handler. You need to limit the loop to run from 0 to 27. There is another problem with your code which I did not see last time around: You initialize maxValid to "false", but then you test for "true" inside your loop (and set it to "false" if it was true). You don't have a valid value yet on the first iteration, so you need to check for "false" and set the variable to "true" inside the "if" block.

2 replies

Inspiring
July 21, 2016

One possible script for the custom calculation option is:

// array of field names to find the max/min value;
var aNames = new Array(
"Number.0", "Number.1", "Number.2", "Number.3",
"Number.4", "Number.5", "Number.6", "Number.7",
"Number.8", "Number.9", "Number.10", "Number.11",
"Number.12", "Number.13", "Number.14", "Number.15",
"Number.16", "Number.17", "Number.18", "Number.19",
"Number.20", "Number.21", "Number.22", "Number.23",
"Number.24", "Number.25", "Number.26", "Number.27"
);
// array of values to be processed;
var aValues = new Array();
var oTempField
// add field values to array of values to be processed;
for(var i =0; i < aNames.length; i++)
{
  oTempField = this.getField(aNames);
  if(oTempField == null)
  {
   app.alert("Error accessing field " + aNames + "\nPlease check the existence of field.", 1, 0);
}
  if(oTempField != null && oTempField.valueAsString != "" && isNaN(oTempField.valueAsString) == false)
  {
   aValues.push(oTempField.value);
  } // end valid number;
} // end field name loop;
// clear the result;
event.value = "";
// determine the maximum value there are values in the aValues array;
if(aValues.length > 0)
{;
  // apply Math.max to each element in the array of values
  event.value = Math.max.apply(null, aValues);
}

Another approach could include the filter method of an array processing each pair of value in the array;

Karl Heinz  Kremer
Community Expert
July 20, 2016

You can use the "Math.max()" function to do that:

Math.max() - JavaScript | MDN

All you need to do is call the function with all your values, or use the array approach outlined in the link above:

var theMax = Math.max(Number(this.getField("Field1").value), Number(this.getField("Field2").value), Number(this.getField("Field3").value), ...);

Just keep in mind that if you don't have a number (a "not a number"), or an empty field, this will not work correctly. I would assemble an array of just valid field values and then use that array as input to Math.max().

rakeshk21205956
Inspiring
July 20, 2016

Sir,

Your Math.max() method didnot worked , i am asking for pdf form javascripting, well i got something for pdf forms but this is not working.... since you are an expert,  could you plz help me with the following script and edit the below script so that this works. My purpose is to find the greatest number among the 28 numbers populated by user my text field is name as  DP.1, DP.2 ........ DP.28 and it should ignore the empty fields

// custom calculation script for "Min"

var minValid = false;

var min = 0;

for (var i=0; i>=4; i++)

{

var val = parseInt(this.getField("DP." + i).value);

if (!isNaN(val)) // we have a valid number

{ if (minValid == true)

{ // this is the first valid value

min = val;

minValid = false;

}

else

{

min = Math.min(min, val);

}

}

}

if (minValid)

event.value = min;

else

event.value = "";

plz edit this javascript to suit my needs

Karl Heinz  Kremer
Community Expert
July 20, 2016

You cannot copy&paste your way to a working JavaScript solution. You need to understand what the script is doing in order to modify it based on your needs. There is a serious problem with the for loop in your code, which causes the loop to never actually be executed. Take a look at this line:

for (var i=0; i>=4; i++)

Now check the syntax for the for loop in JavaScript: Loops and iteration - JavaScript | MDN

As you can see, the second part of the for loop syntax (the "condition") is used to find out for how long the for loop gets executed, when that condition returns "false", the for loop stops. You start the loop with a value of 0, but you test for a value equal or greater than 4 - that will always fail for your initial value of 0, so the loop never gets executed. Also, your field names start with an index of 1, not 0, so even if the loop would execute, the first call to getField() would return a null value and your attempt to get the "value" property would result in an exception which you are not catching.

To get things started, use the following code to run your for loop:

for (var i=1; i<=28; i++)

And of course, you have to change the Math.min() call to a Math.max() to get the maximum.

Do yourself a favor and learn enough JavaScript so that you at least know what you are copying and pasting. As Try67 suggested, we are not here to do your work, we can point you in the right direction and look for errors in your code, but you will have to do some of the work.