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 8 of 8
  1. #1
    Lau
    Lau is offline
    New Coder
    Join Date
    Jan 2004
    Location
    Malden, MA
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Switch does seem to work!

    Hi everyone,

    I seem to be having a problem with a switch/case statement. Here's the function:
    The function is basically supposed to get the hours worked in a week and set the right variables with the right values. Execpt h, all those variables are global.
    Code:
      function setHours() {
        var h;
    	for (var i = 0; i < 7; i++) {
          h = parseInt(document.EmpInfo['day'+i].value);
    	  switch (i) {
    	    case 0: case 1: case 2: case 3: case 4:
              if ( h > 8) {
    	        OT_hours =+ (h-8);
    	        reg_hours =+ 8;
    	      } else
    	        reg_hours =+ h;
    		  break;
    	    case 5:
    	      sat_hours =+ h;
    		  break;
    	    case 6:
    	      sun_hours =+ h;
    		  break;
    	  } // end switch
        } //end for
    	alert('reg hours: ' + reg_hours + ' -- OT hours ' + OT_hours + ' -- sat hours ' + sat_hours + ' -- Sunday hours ' + sun_hours);
      } // end setHours
    I'm not getting the correct values at all with the if statement when h>8.
    you can also check it out here

    Thanks in advance

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Try changing =+ to +=...
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Lau
    Lau is offline
    New Coder
    Join Date
    Jan 2004
    Location
    Malden, MA
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Liorean - it's not working but the output changes... Guess I'm getting somewhere.
    Here's what happens now: if I get rid of the parseInt(), and input something like 8 hours on one day, 5 hours on another, I'll get 85 hours.
    If I keep the parseInt(), I get NaN in the alert that comes up!!

    What's the difference between =+ and +=??

  • #4
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Implementing Lioreans suggestion worked fine for me.
    Although, the way it is written. Saturday and Sundays hours are not included in the overtime.
    Code:
      function setHours() {
        var h;
    	for (var i = 0; i < 7; i++) {
          h = parseInt(document.EmpInfo['day'+i].value);
    	  switch (i) {
    	    case 0: case 1: case 2: case 3: case 4:
              if ( h > 8) {
    	        OT_hours += (h-8);
    	        reg_hours += 8;
    	      } else
    	        reg_hours += h;
    		  break;
    	    case 5:
    	      sat_hours += h;
    		  break;
    	    case 6:
    	      sun_hours += h;
    		  break;
    	  } // end switch
        } //end for
    	alert('reg hours: ' + reg_hours + ' -- OT hours ' + OT_hours + ' -- sat hours ' + sat_hours + ' -- Sunday hours ' + sun_hours);
      } // end setHours
    .....Willy

  • #5
    Lau
    Lau is offline
    New Coder
    Join Date
    Jan 2004
    Location
    Malden, MA
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Willy - this is an assignment. Saturdays and Sundays have different rates, etc. so I didn't include them in the regular overtime count.

    it's till not working for me.. I'm getting really frustrated. Esppecially that you guys tell me it works with you!!

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    += is an addition-and-assignment operator
    Code:
    a += b; // => a = (a + b)
    =+ is two operators, + is the binary addition/unary positive operator and = is a regular assignment operator
    Code:
    a =+ b; // => a = (+b)
    Try adding a second argument, 10, to the parseInt and see if that works any better.
    Last edited by liorean; 02-07-2004 at 10:15 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    Lau
    Lau is offline
    New Coder
    Join Date
    Jan 2004
    Location
    Malden, MA
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I really don't get it... I've tried almost everything.. I still get NaN and undefined in the alert message!
    I think I'll stop for tonight cause I'll end up banging my head on the wall very soon...

  • #8
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It looks as if you also changed your global variables which leaves
    var reg_hours initialy undefined and therefore you are trying
    to add your values to undefined resulting in NaN

    eg:

    To:
    <script language="Javascript">
    var wk_hours, OT_hours, sat_hours, sun_hours = 0;

    var wk_sal, OT_sal, sat_sal, sun_sal = 0;
    var hourly_pay = 0;

    var emp_dept = "";
    var emp_name = "";
    var submitOK;


    From:
    <script type="text/javascript">
    var reg_hours = 0;
    var OT_hours = 0;
    var sat_hours = 0;
    var sun_hours = 0;

    var reg_sal = 0;
    var OT_sal = 0;
    var sat_sal = 0;
    var sun_sal = 0;
    var hourly_pay = 0;

    var emp_dept = "";
    var emp_name = "";
    var submitOK;


    Also; It probably would be best to add a conditional statement that
    if the values of [day+i] is empty, that a default value of 0 is declared.
    (using an onkeyup event handler to allow only numerics wouldn't hurt either)

    function setHours() {
    var h;
    for (var i = 0; i < 7; i++) {
    if(document.EmpInfo['day'+i].value == ''){
    document.EmpInfo['day'+i].value = 0;
    }

    h = parseInt(document.EmpInfo['day'+i].value);
    switch (i) {
    case 0: case 1: case 2: case 3: case 4:
    if ( h > 8) {
    OT_hours += (h-8);
    reg_hours += 8;
    } else
    reg_hours += h;
    break;
    case 5:
    sat_hours += h;
    break;
    case 6:
    sun_hours += h;
    break;
    } // end switch
    } //end for
    alert('reg hours: ' + reg_hours + ' -- OT hours ' + OT_hours + ' -- sat hours ' + sat_hours + ' -- Sunday hours ' + sun_hours);
    } // end setHours


    .....Willy


  •  

    Posting Permissions

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