8 Replies Latest reply on Jul 29, 2015 3:56 PM by gkaiseril

# custom calculations questions

I am working on a time calculation sheet for some employees and i am having some issues.

to start I have the employees starting time say 7:30 am but if they come late it is a deduction which i have working, however if they come in at 7 am then they should get the extra time. and i cant figure out how to do that.  here is the code:

```var one = this.getField("ClockInM");
var two = this.getField("ClockInOK");

if (one.value >= "8" && one.value <= "22"){
event.value = (".25");
two.value = "";
}
else if (one.value >= "23" && one.value <= "37"){
event.value = (".50");
two.value = "";
}
else if (one.value >= "38" && one.value <= "52"){
event.value = (".75");
two.value = "";
}
else if (one.value >= "53" && one.value <= "67"){
event.value = ("1");
two.value = "";
}
else if (one.value <= "-8" && one.value >= "-22"){
event.value = ("+.25");
two.value = "Yes";
}
else{
event.value = ("");
two.value = "Yes";
}

```

second I have some time calculation JavaScript that is not perfect but works. and Try67 made a comment about making it better and i cant seem to find or remember but he said to do. My multiple sclerosis is reeking havoc on my memory, so please be patient and point me in the right direction.

Here is the JavaScript in question which is all over my form.

```var strStart = this.getField("Break1O").value;
var strEnd = this.getField("Break1I").value;
strStart = strStart + " 1/1/70";
strEnd = strEnd + " 1/1/70";

if(strStart.length && strEnd.length){
var timeStart = util.scand("h:MM tt m/d/yy", strStart);
var timeEnd = util.scand("h:MM tt m/d/yy", strEnd);
var diff = timeEnd - timeStart;
var oneHour = 60 * 1000;
var timeleft = (diff/oneHour);
if (timeleft > 10){
event.value = 10
}
else
event.value = timeleft
}
else
event.value = 0

```

I would upload my document only i cant seem to figure out how to do it. I am a beginner with JavaScript so please be thorough with your suggestions.

• ###### 1. Re: custom calculations questions

1. When using numbers do not put them in quotes. If you do that they're considered as strings and mathematical operators like <, >, <=, etc., will not work on them.

2. Remove lines 3 and 4 of your code and change lines 8 and 9 to:

var timeStart = util.scand("h:MM tt m/d/yy", strStart + " " + util.printd("m/d/yy", new Date()));

var timeEnd = util.scand("h:MM tt m/d/yy", strEnd + " " + util.printd("m/d/yy", new Date()));

• ###### 2. Re: custom calculations questions

try67 wrote:

2. Remove lines 3 and 4 of your code and change lines 8 and 9 to:

var timeStart = util.scand("h:MM tt m/d/yy", strStart + " " + util.printd("m/d/yy", new Date()));

var timeEnd = util.scand("h:MM tt m/d/yy", strEnd + " " + util.printd("m/d/yy", new Date()));

Can you please explain why this change is important or what makes it better. I am trying to understand how all this works.

• ###### 4. Re: custom calculations questions

I guess i still don't understand, and also anyone else who is interested in this thread, might find it beneficial to have that explanation. Again I'm not trying to be stubborn, just trying to understand all this. Thank you.

• ###### 5. Re: custom calculations questions

so is the following JavaScript correct? and are the indentations correct?

```var strStart = this.getField("Break1O").value;
var strEnd = this.getField("Break1I").value;

if(strStart.length && strEnd.length){
var timeStart = util.scand("h:MM tt m/d/yy", strStart + " " + util.printd("m/d/yy", new Date()));
var timeEnd = util.scand("h:MM tt m/d/yy", strEnd + " " + util.printd("m/d/yy", new Date()));
var diff = timeEnd - timeStart;
var oneHour = 60 * 1000;
var timeleft = (diff/oneHour);
if (timeleft > 10){
event.value = 10
}
else
event.value = timeleft
}
else
event.value = 0

```
• ###### 6. Re: custom calculations questions

ok so I am getting a syntax error 25 at line 26

```var a = this.getField("Break1Min");
var b = this.getField("Break2Min");
var c = this.getField("Break3Min");
var d = this.getField("EBreak3Min");
var e = this.getField("Break3OK");

if (a.value + b.value == 20){
if (c.value >= 8 && c.value <= 22){
event.value = (.25);
e.value = "";
}
else if (c.value >= 23 && c.value <= 37){
event.value = (.50);
e.value = "";
}
else if (c.value >= 38 && c.value <= 52){
event.value = (.75);
e.value = "";
}
else if (c.value >= 53 && c.value <= 67){
event.value = (1);
e.value = "";
}
else
}
else if (d.value >= 8 && d.value <= 22){
event.value = (.25);
e.value = "";
}
else if (d.value >= 23 && d.value <= 37){
event.value = (.50);
e.value = "";
}
else if (d.value >= 38 && d.value <= 52){
event.value = (.75);
e.value = "";
}
else if (d.value >= 53 && d.value <= 67){
event.value = (1);
e.value = "";
}
else{
event.value = ("");
two.value = "Yes";
}

```

What i am trying to do is if A.value + B.value = 20 then look at C.value and if it fits any of those conditions then event.value would be .25 through 1.

if A.value + B.value does not = 20 then look at d.value and if it fits any of THOSE conditions then event.value would be .25 though 1 depending on the condition it meets.

• ###### 7. Re: custom calculations questions

You have a "dangling" else in that line (25). Remove it.

• ###### 8. Re: custom calculations questions

Rather than using nested "if" statements, consider using the "switch" statement.