...

View Full Version : Switch does seem to work!



Lau
02-07-2004, 10:04 PM
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.

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 (http://www1.coe.neu.edu/~lleccia/ISYG250/proj2/index_proj2.html)

Thanks in advance

liorean
02-07-2004, 10:12 PM
Try changing =+ to +=...

Lau
02-07-2004, 10:22 PM
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 +=??

Willy Duitt
02-07-2004, 10:48 PM
Implementing Lioreans suggestion worked fine for me.
Although, the way it is written. Saturday and Sundays hours are not included in the overtime.

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

Lau
02-07-2004, 11:08 PM
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!!

liorean
02-07-2004, 11:12 PM
+= is an addition-and-assignment operator
a += b; // => a = (a + b)=+ is two operators, + is the binary addition/unary positive operator and = is a regular assignment operator
a =+ b; // => a = (+b)Try adding a second argument, 10, to the parseInt and see if that works any better.

Lau
02-07-2004, 11:39 PM
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...

Willy Duitt
02-08-2004, 01:02 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum