Copy link to clipboard
Copied
I am creating an assessment with 10 questions where a person will have to choose a score between 0 and 4 for each question. This is a standard Likert scale of 0=never, 1=almost never, 2=sometimes, 3=faily often, 4=very often.
The assessment, however, has some questions that are "positive" questions and some that are "negative" questions. For example, "how often have you felt nervous and stressed" is a negative question while "how often have you felt confident" is a positve question.
Then, when I tally the results within the form, I want to reverse their scores for every positve question such that:
- if they entered 0, what gets calculated in the total is a 4
- if they entered 1, what gets calculated in the total is a 3
- if they entered 2, what gets calculated in the total is still a 2
- if they entered 3, what gets calculated in the total is a 1
- if they entered 4, what gets calculated in the total is a 0
I don't know how to write java script so I'm hoping someone can just give me the actual string of text so I can copy it and paste it into my Adobe form creator.
And I assume this will go into the Custom Calculation Script of the calculate tab in the Text Field Properties.
Thaksn for any help.
Copy link to clipboard
Copied
You could use something like this to reverse score:
Change "Field1","Field2","Field3" (add more fields names as needed) with fields names of which you want to reverse score.
var fields = ["Field1","Field2","Field3"];
var total = 0;
for(var i in fields){
var f = this.getField(fields[i]).valueAsString;
if(f==="0")total+=4;
if(f==="1")total+=3;
if(f==="2")total+=2;
if(f==="3")total+=1;}
Now you can use variable 'total' in calculation with rest of the fields.
Copy link to clipboard
Copied
Thank you for your quick reply. This is helpful but I think there may be something missing.
I have 10 questions, each will have a score from 0 to 4. Of those 10 questions, four of them will be reversed when I do a final tally calculation.
So, in the TOTAL field, I want to add Field1 + Field2 + Field3 + Reverse Field4 + Reverse Field 5 + Field 6 + Reverse Field7 + Reverse Field8 + Field9 + Field10.
using your formula above, how will I write this out?
(And will the code be placed into the "Custom Calculation Script" field of the calculate tab?)
Copy link to clipboard
Copied
Yes, script goes to 'Custom calculation script'.
Try this:
var fields = ["Field4","Field5","Field7","Field8"];
var total = 0;
for(var i in fields){
var f = this.getField(fields[i]).valueAsString;
if(f==="0")total+=4;
if(f==="1")total+=3;
if(f==="2")total+=2;
if(f==="3")total+=1;}
var f1= Number(this.getField("Field1").valueAsString);
var f2= Number(this.getField("Field2").valueAsString);
var f3= Number(this.getField("Field3").valueAsString);
var f4= Number(this.getField("Field6").valueAsString);
var f5= Number(this.getField("Field9").valueAsString);
var f6= Number(this.getField("Field10").valueAsString);
var pos = f1+f2+f3+f4+f5+f6;
event.value = total+pos;
Copy link to clipboard
Copied
I am trying it now...thank you. I'll write back in a few minutes. Something isn't workign right but it might be on my end. I'll write back soon.
Copy link to clipboard
Copied
Okay, I just tested it and confirmed that it is not working.
I copied and pasted your commands string into the TOTAL field Custom Caluculation Script.
When I save teh file and then use it as a PDF Form, entering in values for all 10 fields, the TOTAL box remains empty. No addition happens.
Attached is a screen shot.
Copy link to clipboard
Copied
Check the JS Console (Ctrl+J) for errors. If you can't figure it out, share the file with us, please.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
It's not my code, but it should work, but it's generating an error, which you can see in the Console after editing the value of one of the fields. It says:
TypeError: this.getField(fields[i]) is null
4:Field:Calculate
This means you specified an incorrect field name in your script. Remember JS is case-sensitive, so make sure you spell the field names in the code EXACTLY as they appear in the file.
Copy link to clipboard
Copied
PS. Your job (and the usability of the form) will be much easier if you used drop-downs with the values 1-4, instead of open text fields...
Copy link to clipboard
Copied
Thanks Try67. I think what you are saying is the JS Code error is poitning to incorrectly named fields...yes?
Here is a screen shot where all the field names are on the right-hand side of the screen.
The script that was provided had "field1"
Should it just be "1" because the name of that field is only the number 1
Copy link to clipboard
Copied
Hello everyone!
I've been following this thread to try to troubleshoot a custom calculation of my own to no avail and am wondering if you might help. I've attached my form. I'm working on getting the total scores for each chart on page 2 and 3 to calculate a total where half of the responses are reverse scored, and half are not. Here's my attempt for the "consciousness total" caluclation.
var fields = ["Careless","Disorganized","Inefficient","Sloppy"];
var total = 0;
for(var i in fields){
var f = this.getField(fields[i]).valueAsString;
if(f==="1")total+=9;
if(f==="2")total+=8;
if(f==="3")total+=7;
if(f==="4")total+=6;
if(f==="5")total+=5;
if(f==="6")total+=4;
if(f==="7")total+=3;
if(f==="8")total+=2;
if(f==="9")total+=1;
}
var f1= Number(this.getField("Efficient").valueAsString);
var f2= Number(this.getField("Organized").valueAsString);
var f3= Number(this.getField("Practical").valueAsString);
var f4= Number(this.getField("Systematic").valueAsString);
var pos = f1+f2+f3+f4+f5+f6+f7+f8;
event.value = total+pos;
Copy link to clipboard
Copied
You didn't define variables f5,f6,f7,f8.
Copy link to clipboard
Copied
Thanks! Even when I did try that, it comes up NaN. Is it because my inputs are drop-downs?
var fields = ["Careless","Disorganized","Inefficient","Sloppy"];
var total = 0;
for(var i in fields){
var f = this.getField(fields[i]).valueAsString;
if(f==="1")total+=9;
if(f==="2")total+=8;
if(f==="3")total+=7;
if(f==="4")total+=6;
if(f==="5")total+=5;
if(f==="6")total+=4;
if(f==="7")total+=3;
if(f==="8")total+=2;
if(f==="9")total+=1;
}
var f1= Number(this.getField("Efficient").valueAsString);
var f2= Number(this.getField("Organized").valueAsString);
var f3= Number(this.getField("Practical").valueAsString);
var f4= Number(this.getField("Systematic").valueAsString);
var f6= Number(this.getField("Careless").valueAsString);
var f7= Number(this.getField("Disorganized").valueAsString);
var f6= Number(this.getField("Inefficient").valueAsString);
var f8= Number(this.getField("Sloppy").valueAsString);
var pos = f1+f2+f3+f4+f5+f6+f7+f8;
event.value = total+pos;
Copy link to clipboard
Copied
You are still missing a f5 variable, and you have two f6 variables.
Copy link to clipboard
Copied
Ooops, okay. I fixed that but if the calculation were wokring correctly, If I set all the 4 reverse score dropdowns to 9, which would be equal to 1 and leave the rest of the drop downs set to 1, then the total should be 8, It's displaying 44. It seems unaffected entirely by changing the drop-down input.
Copy link to clipboard
Copied
That's how math works, š no matter what numbers you change from first 4 fields you will get 44, because that's how you set it up.
Total = 4, pos = 4+36 = 44, because pos variable adds 4*9 from first fields 9=1 only in total variable.
Copy link to clipboard
Copied
Thanks for your hellp. I guess I'm out of my depth with what I'm trying to do then. I want reverse scoring and addition, no multiplication. I'd like the "Careless","Disorganized","Inefficient","Sloppy" to be reverse whatever is inputted to the drop-down but it's not currently calculating that way from what you're saying. If a user inputs a 9, i'd like it to be added to the calculation as a 1. If they input an 8, it would be added to the calculation as a 2... and so on. The "Efficient","Organized","Practical" and "Systematic" fields would all be addded to the total based on the exact drop down number chosen, no reverse scoring. Then I'd like the total to be addition of all the fields.
Copy link to clipboard
Copied
There are no multiplications, and I believe it does calculate correctly.
"Careless","Disorganized","Inefficient","Sloppy" lets say you select 9 for each of those fields
and "Efficient","Organized","Practical" and "Systematic" 1 for each of those fields, now 'total' variable will reverse all four 9 to 1 so total = 4, 'pos' will calculate 9+9+9+9+1+1+1+1 = 40 , total +pos = 44.
'pos' variable calculate 9 +9+9+9 because that is field's value it doesn't reverse it like 'total' does. Now if you change one of the 9 to 8 total will be 1+1+1+2=5 and pos will be 8+9+9+9+1+1+1+1=39 , 5+39 = 44,
No matter what number you choose from "Careless","Disorganized","Inefficient","Sloppy" you will get 44, only when you change numbers in "Efficient","Organized","Practical" and "Systematic" you will get different result but still then that result will not change event if you keep changing numbers from "Careless","Disorganized","Inefficient","Sloppy", because pos will always calculate it as 10, for example, if total =1 that means value of the field is 9 so pos is 10, if you select 4 total = 6 but real value is 4 so pos is 10 again, you see where this is going?
Copy link to clipboard
Copied
If I set Careless, Disorganized, Inefficient and Sloppy drop-downs to 9, the output I need is 1 for each of them, If I keep the remaining fields at 1 then the total it should caluclate is 8. At the moment it calculates at 40. The range of scores should be 8 to 72. Is it the pos vaiable that's the issue?
Copy link to clipboard
Copied
Every drop-down inputted should change the total score calculated.
Copy link to clipboard
Copied
In that case, just give items in dropdown export value for 9 set export value 1, 8 export value 2...etc then just sum numbers in total same as pos.
Copy link to clipboard
Copied
Then just remove the reverse scoring?
var f1= Number(this.getField("Efficient").valueAsString);
var f2= Number(this.getField("Organized").valueAsString);
var f3= Number(this.getField("Practical").valueAsString);
var f4= Number(this.getField("Systematic").valueAsString);
var f5= Number(this.getField("Careless").valueAsString);
var f6= Number(this.getField("Disorganized").valueAsString);
var f7= Number(this.getField("Inefficient").valueAsString);
var f8= Number(this.getField("Sloppy").valueAsString);
var pos = f1+f2+f3+f4+f5+f6+f7+f8;
event.value = total+pos;
Copy link to clipboard
Copied
Yes, just also add:
var total = f1+f2+f3+f4;
Copy link to clipboard
Copied
Thanks for all your help, Nesa but I can't get it to work so I'll scrap the project. Take care.