Copy link to clipboard
Copied
Hello,
I am preparing a form with calculations and need assistance on a couple of areas. I am very new to javascript calculations and am a bit confused by some of the abbreviations I'm seeing when trying to look through other discussions on this board. Maybe someone can help give me the exact code I should be using and the box I should enter it into?
1. In several locations, I have the user enter data in a specific column. The data cells are all formatted as numbers and the final cell at the bottom sums them up. That works great, but when I change the "sum" to "average" I get fractional numbers. I want this form to work only in wole numbers and need the "average" to not only display as a whole number (ie Format tab) but also to round up for subsequent calculations. How do I enter a "Custom Calculation Script" in the "Calculate" tab so that my "average" cell is a whole number?
2. User's are not required to enter data on every row. The "average" calculation in the "calculate " tab includes all cells that I've selected to be included in the calculation but does not omit empty cells from the calculation and the result is incorrect. How do I omit empty cells from the "average" calculation?
Thank you!!
Copy link to clipboard
Copied
I have found the following ocde and tried several times to get it to work, but to no avail. I just get a blank text box. What am I doing wrong?
Here's the code I'm trying to use to get the average. I haven't tried to add code yet to round the answer - though I need to.
//Initialize variables
var i, v, num = 0, sum = 0;
// Loop through the input fields
for (i=1; i<=7; i++) {
v = +getField("table2_"+i).value;
if (v !== 0) {
// increment the non-blank/zero field count
num++;
// add the field value to the running total
sum +=+f.value;
}
}
// Calculate the average
if (num) {
event.value=sum/num;
} else {
// ALL fields are empty, so set to blank
event.value = "";
}
I have a total of 15 rows (though I've been experimenting with just 7 rows since I'm learning) and I've named them all "table2_row 1","table2_row2","table2_row3" etc to row 7 (I'll make it 15 when I get the formula right).
I'm entering the code into the "custom calculation script" on the "calculate" tab of the "Text Field Properties" box for the filed I'm trying to populate. Is that the right place? Should I be entering this under the Javascript tool? I have multiple sections on this form that require separate calculations so I don't want to get them confused.
Anyone know how to help?
Copy link to clipboard
Copied
Check the Javascript console for errors.
Copy link to clipboard
Copied
I don't get a syntax error but do get a "Warning: JavaScript Window - " which tells me that "The value entered does not match the format of the field [text9]".
I've tried reformatting the text9 box but not sure what I'm missing as it's already set to a number.
Copy link to clipboard
Copied
What happens when you change the format to None?
Copy link to clipboard
Copied
Nothing changes. I think I found someone in the office to take over this form. Whew! I've already spent hours trying to figure this out. Thanks for your help!
Copy link to clipboard
Copied
The code above will not omit blanks, but treat them as zero.
Copy link to clipboard
Copied
Thanks for your help! I eventually found someone in my office IT department to help out with the code. It seemed like such a simple calculation, but getting the code right was elusive for me. Luckily IT was able to fix it. Thanks again!!