Skip to main content
Known Participant
August 24, 2017
Answered

count number of specific answers

  • August 24, 2017
  • 1 reply
  • 1234 views

Hi,

I've created a PDF form with yes/no questions. In the last field I want to see the summary of the amount of 'ja' (= yes) answers.

The field ID's range from Tekstveld 1 (= Text Field) to Tekstveld 37, with no. 38 as the 'total' field.

I've found a Java code and tried to edit it to my needs, but I'm not getting it...

// Initialize counter variable

var sum = 0;

// Loop through the fields

for (var i = 1; i < 37; i += 1) {

// Add one if Teksveld is set to ja

    if (getField("Tekstveld" + i).value !== "ja") {sum += 1;}

}

// Set this field's value to the sum

event.value = sum;

Can someone help me to get it right?

Thanks!

This topic has been closed for replies.
Correct answer try67

I've tried different ways, none of them seem to be working.

this is the file: Dropbox - Website-effectiviteitstest.pdf


As I suggested, there's a space between the text and the number in all of the field names... Use this code:

// Initialize counter variable

var sum = 0;

// Loop through the fields

for (var i = 1; i <= 37; i += 1) {

// Add one if Tekstveld is set to ja

    if (getField("Tekstveld " + i).valueAsString.toLowerCase()=="ja") {sum += 1;}

}

// Set this field's value to the sum

event.value = sum;

Also, for some reason you've set the Format of the total field as "Zip code". Change it to None or to Number.

I would also recommend that you use a check-box or at least a drop-down instead of open text fields.

If someone enters "JA", for example, or "Ja", or "X", or anything else instead of "ja", the script above will not count it, which is not good.

Edit: added the code, and fixed it so that it will recognize all versions of "ja", regarding of upper/lower-case letters.

1 reply

try67
Community Expert
Community Expert
August 25, 2017

- You need to change i<37 to i<=37

- You need to change value !== "ja" to value=="ja"

The rest seems fine.

Known Participant
August 25, 2017

Hi try67,

Thanks for responding so quickly

Still having issues though, I still must be doing something wrong. This is the new code:

// Initialize counter variable
var sum = 0;
// Loop through the fields
for (var i = 1; i <= 37; i += 1) {
// Add one if Tekstveld is set to ja
    if (getField("Tekstveld" + i).value== "ja") {sum += 1;}
}
// Set this field's value to the sum
event.value = sum;

If this code is correct, maybe I'm adding the code to wrong place? This is where I added the code: Properties -> Calculate -> Custom calculation script

Known Participant
August 25, 2017

As I suggested, there's a space between the text and the number in all of the field names... Use this code:

// Initialize counter variable

var sum = 0;

// Loop through the fields

for (var i = 1; i <= 37; i += 1) {

// Add one if Tekstveld is set to ja

    if (getField("Tekstveld " + i).valueAsString.toLowerCase()=="ja") {sum += 1;}

}

// Set this field's value to the sum

event.value = sum;

Also, for some reason you've set the Format of the total field as "Zip code". Change it to None or to Number.

I would also recommend that you use a check-box or at least a drop-down instead of open text fields.

If someone enters "JA", for example, or "Ja", or "X", or anything else instead of "ja", the script above will not count it, which is not good.

Edit: added the code, and fixed it so that it will recognize all versions of "ja", regarding of upper/lower-case letters.


Yes!! It works

That zipcode setting must have come up when I was testing locations... silly of me to have left it there.

Thanks a lot, I really appreciate your help!!