PDA

View Full Version : Possible Logic error?



celica1010
04-30-2014, 03:32 PM
Hello to all

I am trying to calculate pay in three separate boxes. I am using an if-statement for OT or non-OT and my values are not correct with the logic i have in the statement. The else works fine but I cannot figure out why the if doesnt work when there is OT. Formatting of page is exactly how I want it so I gotta think i am missing something very simple.



<!DOCTYPE html

PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Gross Pay</title>



<script type="text/javascript">
function overtime(hours, rate) {

if (hours > 40) {
otHrs = (hours - 40);
otRate = (otHrs * 0.5);
gross = (hours + otRate) * rate;
}
else {
gross = hours * rate;
}
return gross;
}
function doSomething()

{

var in1 = document.getElementById("input1").value;

var in2 = document.getElementById("input2").value;

var out1 = document.getElementById("output");

var in3 = document.getElementById("input3").value;

var in4 = document.getElementById("input4").value;

var out2 = document.getElementById("output2");

var in5 = document.getElementById("input5").value;

var in6 = document.getElementById("input6").value;

var out3 = document.getElementById("output3");


out1.value = overtime(in1, in2);
out2.value = overtime(in3, in4);
out3.value = overtime(in5, in6);

}

</script>

</head>

<body>

<table>
<tr>
<td></td>
<td>Employee 1</td>
<td>Employee 2</td>
<td>Employee 3</td>

</tr>

<tr>

<td>Hours</td>

<td><input type="text" id="input1" name="input1"/></td>
<td><input type="text" id="input3" name="input3"/></td>
<td><input type="text" id="input5" name="input5"/></td>


</tr>

<tr>

<td>Rate</td>

<td><input type="text" id="input2" name="input2"/></td>
<td><input type="text" id="input4" name="input4"/></td>
<td><input type="text" id="input6" name="input6"/></td>

</tr>

<tr>

<td>Gross Pay</td>

<td><input type="text" id="output" name="output" readonly="readonly"/></td>
<td><input type="text" id="output2" name="output2" readonly="readonly"/></td>
<td><input type="text" id="output3" name="output3" readonly="readonly"/></td>

</tr>

<tr>

<td colspan="6" style="text-align:center;">

<button type="button" onclick="javascript:doSomething();">Calculate</button>

</td>

</tr>

</table>

</body>

</html>




Thoughts?

sunfighter
04-30-2014, 04:00 PM
Mistake in the oT calculation:

function overtime(hours, rate) {
if (hours > 40) {
otHrs = (hours - 40);
otRate = (rate * 1.5);
gross = (otHrs * otRate) + (40 * rate);
}else {
gross = hours * rate;
}
return gross;
}

Philip M
04-30-2014, 04:09 PM
hours is a string value, not a number until converted. So this line
gross = (hours + otRate) * rate;
concatenates two strings, not adds two numbers.

Add this line:-


function overtime(hours, rate) {
hours = Number(hours);


Quizmaster: Where does an arboreal animal live?
Contestant: In Arboria.

celica1010
05-13-2014, 06:55 AM
Made sense thanks!!!