Web Analytics Made Easy -
StatCounter Switch does seem to work! - CodingForum

Announcement

Collapse
No announcement yet.

Switch does seem to work!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    Try changing =+ to +=...
    liorean <[[email protected]]>
    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

    Comment


    • #3
      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 +=??

      Comment


      • #4
        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

        Comment


        • #5
          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!!

          Comment


          • #6
            += 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; Feb 7, 2004, 05:15 PM.
            liorean <[[email protected]]>
            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

            Comment


            • #7
              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...

              Comment


              • #8
                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

                Comment

                Working...
                X