Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Payroll Type Java Script

    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

    Code:
    <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>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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
    Last edited by Philip M; 01-10-2012 at 05:04 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    you need to get the values from those input boxes, like this:
    Code:
    nForm.inHr.value;
    , etc

  • #4
    New Coder
    Join Date
    Jan 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    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.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by jmakejew View Post
    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).

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    New Coder
    Join Date
    Jan 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    never mind...
    Last edited by xelawho; 01-10-2012 at 05:39 PM. Reason: superfluous commentary


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •