Skip to main content
Participant
April 26, 2020
Question

Calculating hours only relevant hours to a category

  • April 26, 2020
  • 3 replies
  • 496 views

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.
I may be going about this the completely wrong way.. any help would be appreciated. Thanks

 

//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 = "";

This topic has been closed for replies.

3 replies

Participant
April 28, 2020

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";

try67
Community Expert
Community Expert
April 28, 2020

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

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

else event.value = 0;

Participant
May 4, 2020

Thanks try67, the others are needed for variable day entries e.g if AL = annual leave, TR = training etc I then have to take off lunch breaks etc it's all working well now, thanks again. Much appreciated

try67
Community Expert
Community Expert
April 26, 2020

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

Bernd Alheit
Community Expert
Community Expert
April 26, 2020

Check the console for errors.