...

View Full Version : Payroll Type Java Script



jmakejew
01-10-2012, 04:27 PM
I am trying to write a Java Script code, that will calculate hours worked in a day. whenever I try to test the code I get NA in the answer field. Thanks for your help

Jason


<html>
<head>
<script type="text/javascript">

function calc(nForm){

var inHour = nForm.inHr;
var inMinutes = nForm.inMin;
var lunchOutHour = nForm.lunchOutHr;
var lunchOutMinutes = nForm.lunchOutMin;
var lunchInHour = nForm.lunchInHr;
var lunchInMinutes = nForm.lunchInMin;
var outHour = nForm.outHr;
var outMinutes = nForm.outMin;


var linearInTime = 60*(inHour)+(inMinutes);
var linearLunchOutTime = 60*(lunchOutHour)+(lunchOutMinutes);
var linearLunchInTime = 60*(lunchInHour)+(lunchInMinutes);
var linearOutTime = 60*(outHour)+(outMinutes);

if (linearLunchOutTime < linearInTime || linearLunchInTime < linearLunchOutTime || linearOutTime < linearLunchInTime){alert('Invalid Information');return false}

var workDay = linearOutTime-linearInTime;
var lunchPeriod = linearLunchInTime-linearLunchOutTime;
var linearMinutesWorked = workDay-lunchPeriod;

var hoursWorked = parseInt(linearMinutesWorked/60);
var minutesWorked = linearMinutesWorked-(hoursWorked*60)

if (hoursWorked < 10){hoursWorked = "0"+hoursWorked}
if (minutesWorked < 10){minutesWorked = "0"+minutesWorked}

nForm.timeWorked.value = hoursWorked+":"+minutesWorked;
}

</script>
</head>
<body>
<form name='timeSheet'>
Clock In:
<label>
<input name="inHr" type="text" id="inHr" size="10">
</label>
&nbsp
<label>
<input name="inMin" type="text" id="inMin" size="10">
</label>
<br>
Lunch Out:
<label>
<input name="lunchOutHr" type="text" id="lunchOutHr" size="10">
</label>
&nbsp
<label></label>
<input name="lunchOutMin" type="text" id="lunchOutMin" size="10">
<br>
Lunch In:

<label>
<input name="lunchInHr" type="text" id="lunchInHr" size="10">
</label>
&nbsp
<label>
<input name="lunchInMin" type="text" id="lunchInMin" size="10">
</label>
<br>
Clock Out:
<label>
<input name="outHr" type="text" id="outHr" size="10">
</label>
&nbsp
<label>
<input name="outMin" type="text" id="outMin" size="10">
</label>
<br>
<br>
Time Worked: <input type='text' name='timeWorked' size='5' readonly>
<br><br>
<input type='button' value="Calculate" onClick="calc(this.form)">
</form>
</body>
</html>

Philip M
01-10-2012, 04:41 PM
You must change all the input values to numbers, e.g:-

var inHour = Number(nForm.inHr.value);

You have given your HTML elements ids, but you need to assign names to them. Only IE regards ids as names.

Avoid using unnecessary brackets:-
var linearInTime = 60*(inHour)+(inMinutes);

You have copied this script from this forum and the original used select lists for the times - and worked perfectly. Why have you tried to alter it? You now have no protection against silly values being entered: 47:87

xelawho
01-10-2012, 04:47 PM
you need to get the values from those input boxes, like this:

nForm.inHr.value;, etc

jmakejew
01-10-2012, 05:05 PM
You must change all the input values to numbers, e.g:-

var inHour = Number(nForm.inHr.value);

You have given your HTML elements ids, but you need to assign names to them. Only IE regards ids as names.

Avoid using unnecessary brackets:-
var linearInTime = 60*(inHour)+(inMinutes);

You have copied this script from this forum and the original used select lists for the times - and worked perfectly. Why have you tried to alter it? You have no protection against silly values being entered: 27:87

Thanks,

I know but that's the way my manager wants it. We are making it so they can input the number directly from the punch card.

Philip M
01-10-2012, 05:11 PM
Thanks,

I know but that's the way my manager wants it. We are making it so they can input the number directly from the punch card.

I am always amazed that commercial organisations seem to want to employ unqualified people to write computer code. Presumably your manager would not attempt to service or repair a machine or appliance without any experience or understanding of how it worked (or assign an employee to that).

jmakejew
01-10-2012, 05:30 PM
Not really here to write computer codes, just doing it as a favor for the person ahead of our payroll department here.. Thanks for your help though.

Btw, I'm learning as I go, and I know a little bit more how to write java script then I did yesterday.

xelawho
01-10-2012, 05:38 PM
never mind...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum