Help with Javascript with if else if statement
Copy link to clipboard
Copied
I've found a few posts that seem like they should work for me but I keep getting a SyntaxError: missing ; before statement 1: at line 2
My form has 9 sections. Each section has 6 - 16 sub-sections with dropdown fields. The options to select are: Met, Not Met, and Exceeded. I have given them values of 1, 3, and 6, respectively.
The sum of each section is being captured in field: Objective 1 Score. (I am using the Calculate > Value is the Sum of the following fields option). This field will be hidden as I'm only using it to capture the Score. This field is calculating properly.
I want the Score visible to the user to read as either Met, Not Met, or Exceeded, depending on the value calculated above.
- ≤ 6 = "Not Met"
- 7-18 = "Met"
- >19 = "Exceeded"
Here's my script under Calculate > Custom calculation script
var Score 1 = "Objective 1 Score";
if ( event.value < 6){
Score 1 = "Not Met";
} else if (( event.value >= 7) && ( event.value < 18)){
Score 1 = "Met";
} else if (event.value >=19){
Score 1 = "Exceeded";
}
Like I said, I get the syntax error. I haven't been able to actually test this yet.
Copy link to clipboard
Copied
In your code
var Score 1 = "Objective 1 Score";
there is a space in "Score 1". There can be no spaces in variable names.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
The only value I'm receiving back is Not Met. Thoughts?
Copy link to clipboard
Copied
Here's my new function
var Score1 = "Objective 1 Score";
if ( event.value < 6){ event.value = "Not Met";
} else if (( event.value > 7) && ( event.value < 18)){
event.value = "Met"; }
else if (event.value > 19){
event.value = "Exceeded";
}
Copy link to clipboard
Copied
Score is 27, should be Exceeded
Copy link to clipboard
Copied
Is this the calculation script for "Score 1"? If so, change these lines:
Score 1 = "Not Met";
To this:
event.value = "Not Met";
Copy link to clipboard
Copied
Good Point Try!!
So also read this:
https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Hi, thank you.
Here's my new function
var Score1 = "Objective 1 Score";
if ( event.value < 6){ event.value = "Not Met";
} else if (( event.value > 7) && ( event.value < 18)){
event.value = "Met"; }
else if (event.value > 19){
event.value = "Exceeded";
}
Its only giving me "Not Met: return value. Screenshot below shows the score at 27, should be Exceeded.
Copy link to clipboard
Copied
I actually changed it to:
var Score1 = "Objective 1 Score";
if (( event.value > 1) && ( event.value < 6)){
event.value = "Not Met";
} else if (( event.value > 7) && ( event.value < 18)){
event.value = "Met"; }
else if (event.value > 19){
event.value = "Exceeded";
}
Where would I put a line to say if 0 keep blank?
Copy link to clipboard
Copied
Add an "else" by itself to catch all the cases that are not already acounted for.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Thanks Thom,
Still having an issue with the field Score1 (gray cell with 0) not displaying correctly.
var Score1 = "Objective 1 Score";
if (( event.value > 1) && ( event.value < 6)){
event.value = "Not Met"; }
else if (( event.value > 7) && ( event.value < 18)){
event.value = "Met"; }
else if (event.value > 19){
event.value = "Exceeded"; }
else {
event.value = 0 // }
Score is 19 so it should display Exceeded in Score1 field but it's only catching that my default is to be 0.
Copy link to clipboard
Copied
Where does you use the script? Why does you set the variable Score1? Where does you get the score?
Copy link to clipboard
Copied
I am essentially trying to do a grading system. I am rethinking my approach on this.
Here's my scope: I have an employee appraisal form. There are 9 objectives total with each having anywhere from 5 - 16 requirements that need to be graded. The rater will score each requirement as either: Not Met, Met, Exceeded, or Not Applicable. From their selections, I want the field "Score1" to display the employee's overall performance in that objective.
For example, Objective 1 has 6 requirements. If 2 were Exceeded, 3 were Met, 1 Not Met, 1 Not Applicable. Score 1 would display "Met" as this reflected more than 50% of the answers.
My initual thought was to assign them values (stored in Objective 1 Score) and try to work it out that way.
Not Met = 1
Met = 3
Exceeded = 6
Not Applicable = 0
Objective 1 Score is averaging the Rating fields.
Score 1 is looking to Objective 1 Score to convert the grade from numerical to Not Met, Met, or Exceeded depending on what range it falls in.
I'm sure there is an easier way. My javascript language is not strong. Please help.
Copy link to clipboard
Copied
It is possible when you use the value of the field "Objective 1 Score".
Copy link to clipboard
Copied
@Bernd Alheit I think what I need is a switch statement. Can you help me write this?
I've changed Objective 1 Score to calculate the average of the ratings.
Here's my new script in Score1. I'm getting a syntax error
const Score1 = Objective1Score().getObjective1Score();
switch (true) {
case: Objective1Score <= 16:
console.log("Not Met");
break;
case: Objective1Score >= 17 || <=149:
console.log("Met");
break;
case: Objective1Score >= 150:
console.log("Exceeded");
break;
default:
console.log(" ");
}
Copy link to clipboard
Copied
This is not valid Acrobat JS code. You can't just invent functions and expect them to work. Nor can you copy&paste code that was written for something else (like a browser) and expect it to work in a different environment.
Copy link to clipboard
Copied
You're script using "if" statments is mostly correct.
But you are missing a couple of details. First, you need to read the content here:
https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm
Now it is critical that the script is written correctly for the specific event where it is placed. It's also important to explain the setup and the desired result simply and clearly.
So you have some options.
Option #1 - Put all functionality into the "Score1" field. For this option the calcualation is the standard built-in Sum that you showed in a previous post. Then you'd add this code to a Custom Format script.
// Custom Format Script
if (( event.value > 1) && ( event.value < 6)){
event.value = "Not Met";
}
else if (( event.value > 7) && ( event.value < 18)){
event.value = "Met";
}
else if (event.value > 19){
event.value = "Exceeded";
}
else {
event.value = 0
}
Option #2 - Split the functionality between two fields, the Score1 field and the "Objective1" field. Use the standard built-in Sum calculation in the "Objective1" field.
Then use this script in the Custom Validate event on the "Objective1" field.
NOTE: THERE ARE NO SCRIPTS IN THE Score1 FIELD
// Custom Format Script
var oScoreFld = this.getField("Score 1");
if (( event.value > 1) && ( event.value < 6)){
oScoreFld.value = "Not Met";
}
else if (( event.value > 7) && ( event.value < 18)){
oScoreFld.value = "Met";
}
else if (event.value > 19){
oScoreFld.value = "Exceeded";
}
else {
oScoreFld.value = 0
}
Use the Acrobat JavaScript Reference early and often