These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Median value calcuation in Javascript

danikeda
Registered: Mar 6 2007
Posts: 8

Javascript Calculation Quetion:
 
How does one calculate the Median value for a a set of neumbers entered into a table array in Adobe Acorbat useing Javacript?

My Product Information:
Acrobat Pro 8, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
What kind of Acrobat Form technology are you using? LiveCycle? or the traditional AcroForms?

While the details are different, Which ever technology you are using the methodology is the same. First you identify all of the fields that are part of the median calculation. The best way to do this is with a field naming convention that uniquely identifies just those field. Neither of Acrobat Forms technologies has a median function so this calculation has to be done with an Algorithm. Fortunately its pretty easy. Walk through the list of fields and insert the field values into a sorted array, Select the index that's half the array length and you've got your median value.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

danikeda
Registered: Mar 6 2007
Posts: 8
Thanks for the tip. I am using Acrobat 8 Pro. I made an attempt at your suggestion. Unfortunately I was never trained in Javascript so I probably did something wrong. I am a physician and trying to create an audit tool that will calculate the medain glucose value from measurments over the first 24 hrs of a patients ICU admission.

I am pastng my attempt. which unfortunately did not work. Any halp on what I did worng?

Thanks
------------------------------------------

var E =24; //total number of input spaces
var N = 0;
var theList = new Array("24hr_Glucose.0.0", "24hr_Glucose.0.1", "24hr_Glucose.0.2","24hr_Glucose.0.3", "24hr_Glucose.0.4", "24hr_Glucose.0.5", "24hr_Glucose.1.0", "24hr_Glucose.1.1", "24hr_Glucose.1.2","24hr_Glucose.1.3", "24hr_Glucose.1.4", "24hr_Glucose.1.5", "24hr_Glucose.2.0", "24hr_Glucose.2.1", "24hr_Glucose.2.2","24hr_Glucose.2.3", "24hr_Glucose.2.4", "24hr_Glucose.2.5", "24hr_Glucose.3.0", "24hr_Glucose.3.1", "24hr_Glucose.3.2","24hr_Glucose.3.3", "24hr_Glucose.3.4", "24hr_Glucose.3.5");

var theMedian = 0.0;



// Run through all the input, add those that have valid values

for(i = 0; i < E; i++) {
if(!isNaN(parseFloat("24hr_Glucose."[i].value))) {
SUM += parseFloat("24hr_Glucose."[i].value);
N++;
}
}//put these values in an array........
var a = 0;
for(i = 0; i < E; i++) {
if(!isNaN(parseFloat("24hr_Glucose."[i].value))) {
theList[a]=parseFloat("24hr_Glucose."[i].value);
a++;
}
}
//sort the list
function compareNumbers(a, b)
{
return a - b
} for(i=0; i
danikeda
Registered: Mar 6 2007
Posts: 8
//sort the list
function compareNumbers(a, b)
{
return a - b
} for(i=0; i
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
You've done great so far for someone who isn't a programmer. There are quite a few errors though and how you acquire field values depends on what kind of forms technology is used. Was this form created in LiveCycle Designer, or were the form fields added from the "Forms" toolbar in Acrobat 8?

Ok, so supposing you can acquire all the field values and push them onto an array, here's the Median calculation

// This is the acquired array of valuesvar vals = [10,2,6,4,3,5]; fucnction MyComp(a,b){var rslt = a-b;return rslt/Math.abs(rslt);} vals.sort;var halfLen = vals.length/2;var nIdx = Math.floor(halfLen);if(nIdx  == halfLen)median = (vals[nIdx+1] - vals[nIdx])/2;elsemedian = vals[nIdx];

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Sorry, I made one mistake. When the sort function is called, it has to include the compare function. The line should be this

vals.sort(MyComp);

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

danikeda
Registered: Mar 6 2007
Posts: 8
Thanks greatly for the help. Unfortunately I have struggled with the problme of inserting my field values into a new array for which a Median value can be calucalted. Attached is the version I think has the fewest mistakes but still does not work:

var theList = ["24hr_Glucose.0.0", "24hr_Glucose.0.1", "24hr_Glucose.0.2","24hr_Glucose.0.3", "24hr_Glucose.0.4", "24hr_Glucose.0.5", "24hr_Glucose.1.0", "24hr_Glucose.1.1", "24hr_Glucose.1.2","24hr_Glucose.1.3", "24hr_Glucose.1.4", "24hr_Glucose.1.5", "24hr_Glucose.2.0", "24hr_Glucose.2.1", "24hr_Glucose.2.2","24hr_Glucose.2.3", "24hr_Glucose.2.4", "24hr_Glucose.2.5", "24hr_Glucose.3.0", "24hr_Glucose.3.1", "24hr_Glucose.3.2","24hr_Glucose.3.3", "24hr_Glucose.3.4", "24hr_Glucose.3.5"];
var vals = new Array();

myArrayFunction(theList);
function myArrayFunction(theList)
{
var N=0;
for ( var i=0;i