Forum Moderators: open

Message Too Old, No Replies

quick question about naming variables

         

mylungsarempty

1:27 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



If i have two functions, with different names of course, can i use variables inside those functions which have the same names? That is, if i have a function, and i simply copy/paste the function so it's on my page twice, then do i need to change the names of the variables as well?

txbakers

2:18 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



variables within different functions can have the same name.

look up documentation on "scope" for a clearer explanation.

txbakers

2:19 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



dupe - sorry

mylungsarempty

2:34 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



alright, i read up on that... it makes sense to me. maybe someone can help me figure out the best way to do this then:

<script type="text/javascript">

function calculate(arg) {

//AM

in_ = arg.am_in.value;
out_ = arg.am_out.value;

in_ampm = in_.substring(in_.length-2);
in_minute = (in_.substring((in_.length-4),3) * 1);
in_hour = (in_.substring(0,(in_.length-4)) * 1) + (in_ampm=="pm"?12:0);

out_ampm = out_.substring(out_.length-2);
out_minute = (out_.substring((out_.length-4),3) * 1);
out_hour = (out_.substring(0,(out_.length-4)) * 1) + (out_ampm=="pm"?12:0);

foobar = new Date();

in_timeObject = new Date(foobar.setHours(in_hour));
in_timeObject = in_timeObject.setMinutes(in_minute);
// in_timeObject is now a Unix timestamp
// let's do the same with the out time
out_timeObject = new Date(foobar.setHours(out_hour));
out_timeObject = out_timeObject.setMinutes(out_minute);
// now you just need to subtract...
timeDiff = (out_timeObject - in_timeObject)/1000;
// timeDiff holds the difference (in seconds) between the "in" and "out" times
time = timeDiff / 60 / 60;

//PM

p_in_ = arg.pm_in.value;
p_out_ = arg.pm_out.value;

p_in_ampm = p_in_.substring(p_in_.length-2);
p_in_minute = (p_in_.substring((p_in_.length-4),3) * 1);
p_in_hour = (p_in_.substring(0,(p_in_.length-4)) * 1) + (p_in_ampm=="pm"?12:0);

p_out_ampm = p_out_.substring(p_out_.length-2);
p_out_minute = (p_out_.substring((p_out_.length-4),3) * 1);
p_out_hour = (p_out_.substring(0,(p_out_.length-4)) * 1) + (p_out_ampm=="pm"?12:0);

foobar = new Date();

p_in_timeObject = new Date(foobar.setHours(p_in_hour));
p_in_timeObject = p_in_timeObject.setMinutes(p_in_minute);
// in_timeObject is now a Unix timestamp
// let's do the same with the out time
p_out_timeObject = new Date(foobar.setHours(p_out_hour));
p_out_timeObject = p_out_timeObject.setMinutes(p_out_minute);
// now you just need to subtract...
p_timeDiff = (p_out_timeObject - p_in_timeObject)/1000;
// timeDiff holds the difference (in seconds) between the "in" and "out" times
p_time = p_timeDiff / 60 / 60;

//TOTAL

arg.expr.value = time
arg.p_expr.value = p_time

arg.total.value = eval((time)+(p_time))

}

</script>

Now, i have this script. What i need to accomplish now is applying this script to 7 days a week for about 50 employees. I'm going to use a form to let someone select each employees name, which will show a new <div> with another form to enter their punch-clock times, and after all the employees time fields are filled out, clicking a button will print out a sheet with each employee's name and their total hours next to it. This is my first JavaScript script, though, so i'm having difficulty figuring out the best way to go about applying this script that i have for over 50 employees, 7 times each... any thoughts to guide me?