4 Replies Latest reply on Feb 24, 2014 5:19 AM by csb102155

# Assigning letter grades to numbers

Hello all, I've a rather unusual (I think it is anyway) request for the form I've been bugging everyone about for the past week.  The owner would like to assign a letter grade to the numbers.  They provided me with the microsoft excel formula that they used, but I don't know how I could or if it could be done, using javascripts.

Here is the formula they used in excel:  =IF(H24<1,"F",IF(H24<2,"D",IF(H24<3,"C",IF(H24<4,"B",IF(H24<5,"A")))))

Is this possible using Javascripts and if so, how would you write the code for this?

Thanks

Connie

• ###### 1. Re: Assigning letter grades to numbers

It's actually quite a common request, and quite easy to implement. Let's say you have a field called "Score" that has the score as a number.

In the other field you enter something like this as the custom calculation code:

var v = this.getField("Score").value;

if (v=="") event.value = "";

else {

v = Number(v);

if (v<1) event.value = "F";

else if (v<2) event.value = "D";

else if (v<3) event.value = "C";

else if (v<4) event.value = "B";

else if (v<5) event.value = "A";

}

• ###### 2. Re: Assigning letter grades to numbers

There are many ways to accomplish this with JavaScript.

Assuming your fields are named the same way as the Excel worksheet:

// custom calculation script for the letter grade ffield;
function GetField(cName) {
var oField = this.getField(cName);
if(oField == null) app.alert("Error accessing field named: " + cName);
return oField;
}

// name for field with score value
cScoreField = "H24";
// get the field value;
var nValue = (GetField(cScoreField)).valueAsString;
var nGrade = "";
console.println(nValue);
// select letter based on score value;
switch(true) {
case (nValue == ""):
nGrade = "";
break;
case (nValue < 1):
nGrade = "F";
break;
case (nValue < 2 ):
nGrade = "D";
break;
case (nValue < 3):
nGrade = "C";
break;
case (nValue < 4):
nGrade = "B";
break;
case (nValue < 5):
nGrade = "A";
break;
default:
nGrade = "";
app.alert("Unknown value: " + nValue);
break;
} // end switch;
event.value = nGrade;

// end custom calculation script;

• ###### 3. Re: Assigning letter grades to numbers

Hi, I tried the script that you provided and it worked fine until I had to change the numbers to decimals.  Can you take a look at https://files.acrobat.com/preview/d2f845b3-2f83-4dbf-90fd-aa2b58a74ea5 and see what I need to do?

Thanks for your help.

Connie

• ###### 4. Re: Assigning letter grades to numbers

Hi Gilad D, I used your script in the document, but then the owner decided to use decimal and for some reason, the script now doesn't work.  Have you got any suggestions?  You can see the file at https://files.acrobat.com/preview/d2f845b3-2f83-4dbf-90fd-aa2b58a74ea5.

Thanks for your help.

Connie