## Calculating hours only relevant hours to a category

New Here ,
Apr 25, 2020

Copied

Hi Community, I can get my code to sum total annual leave when string includes "AL" however, if the string include another category e.g. "SK" for sick leave, I get NaN sum value error. I need help to make my JavaScript to only see strings value associated with AL and then calculate over 5 days e.g vars A to E.

//sum annual leave over working week

//Mon to Fri var's a to e

var a = getField("CHset1").valueAsString;
var b = getField("CHset2").valueAsString;
var c = getField("CHset3").valueAsString;
var d = getField("CHset4").valueAsString;
var e = getField("CHset5").valueAsString;

event.value="";

var parts1 = a.split("AL, ");
var parts2 = b.split("AL, ");
var parts3 = c.split("AL, ");
var parts4 = d.split("AL, ");
var parts5 = e.split("AL, ");

if (parts.length >=2)
{
event.value = (1*parts1[1]) + (1*parts2[1]) + (1*parts3[1]) + (1*parts4[1]) + (1*parts5[1]);
}
else event.value = "";

TOPICS
Acrobat SDK and JavaScript, PDF forms

Views

151

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

## Calculating hours only relevant hours to a category

New Here ,
Apr 25, 2020

Copied

Hi Community, I can get my code to sum total annual leave when string includes "AL" however, if the string include another category e.g. "SK" for sick leave, I get NaN sum value error. I need help to make my JavaScript to only see strings value associated with AL and then calculate over 5 days e.g vars A to E.

//sum annual leave over working week

//Mon to Fri var's a to e

var a = getField("CHset1").valueAsString;
var b = getField("CHset2").valueAsString;
var c = getField("CHset3").valueAsString;
var d = getField("CHset4").valueAsString;
var e = getField("CHset5").valueAsString;

event.value="";

var parts1 = a.split("AL, ");
var parts2 = b.split("AL, ");
var parts3 = c.split("AL, ");
var parts4 = d.split("AL, ");
var parts5 = e.split("AL, ");

if (parts.length >=2)
{
event.value = (1*parts1[1]) + (1*parts2[1]) + (1*parts3[1]) + (1*parts4[1]) + (1*parts5[1]);
}
else event.value = "";

TOPICS
Acrobat SDK and JavaScript, PDF forms

Views

152

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 25, 2020 0
5 Replies 5
Apr 26, 2020

Copied

Check the console for errors.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 26, 2020 0
Most Valuable Participant ,
Apr 26, 2020

Copied

You've never defined a variable called "parts". You need to check each partsX variable separately.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 26, 2020 0
New Here ,
Apr 27, 2020

Copied

Thanks try67 and Bernd, i ended up going back to bricks and mortar vars..

var a = getField("SET1").valueAsString;

var f = getField("HOURS1").valueAsString;

if (a=="AL")event.value = 0;
else if (a=="ISS")event.value = 0;
else if (a=="PH")event.value = 0;
else if (a=="PL")event.value = 0;
else if (a=="PR")event.value = 0;
else if (a=="RP")event.value = f;
else if (a=="SM")event.value = 0;
else if (a=="ST")event.value = 0;
else if (a=="TR")event.value = 0;
else if (a=="K")event.value = 0;

else event.value="0";

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 27, 2020 0
Most Valuable Participant ,
Apr 28, 2020

Copied

You don't need most of that code. Simply use:

if (a=="RP") event.value = f;

else event.value = 0;

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 28, 2020 0
LATEST
New Here ,
May 04, 2020