Javascript - syntax or logic error (or both) ?

Status
Not open for further replies.

oldskool

Oldie but a goodie
Messages
1,760
Location
Maine, USA
As I have stated before with other problems, I am a student, and not looking for the work to be done, but just a hint at what I am doing wrong. Thanks for any help anyone can give !

I am writing a Javascript program to calculate gross pay, and it is supposed to figure in overtime pay as well. Anything 41 hours and up is time and one-half pay.
I have this all working except for one thing, I can't seem to figure out why the overtime feature isn't working. First I will display my code based on straight 40 hours, then I will post the IF statement that I think would work, but I am apparently either not typing it out syntactically correct, or logically correct or both.


Code:
<html>
<!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>Wages HTML</title>
<meta http-equiv="content-type" content="text/html;
     charset=iso-8859-1" />

<br />
<br />
<br />
<br />

<center><p><form name="gross"><h3><b>Please enter your base hourly pay, and click the button below</b><h3>


<h5><i>Enter the # of hours worked this week</i><h5><p>

<script type="text/javascript">
/* <![CDATA[ */

//code written by Eric Maddan


function grossPay(pay)   

{

var grossWage=parseFloat(pay);
var hrsWorked=parseFloat(document.gross.hours.value);
var gross=(grossWage * hrsWorked);

var outBoxtext = ("   $   " + " " + " " + gross) ;
    document.gross.pay.value=outBoxtext;
    
}

</script>

<center>
<input type="text" name="inputText" size="20"><p>

Your hourly wage in dollars and cents is:

<br />
<br />

<input type="text" name="hours" size="20">

<br />
<br />
<br />
<br />

Your pay for this week is :

<br />
<br />

<input name="pay" size="20"><br /><br />
<input type="button" value="Calculate Gross Pay" 
        OnClick="grossPay(inputText.value);"></center>

</form>
</center>
</script>
</head>

<body>

<script type="text/javascript">
/* <![CDATA[ */


/* ]]> */
</script>




</script>

</body>
</html>

And here is the code I am hoping I can insert if it is correct, but I don't know if it is logic or syntax error, or both as I said....

Code:
if (hrsWorked >= 41)   
    {
    hrsWorked = ((hrsWorked + (hrsWorked / 2));
    document.gross.hours.value=hrsWorked;
    }
else
    {
    hrsWorked = hrsWorked;
    }
 
when i'm debugging, I usually display the data in message boxes. in this example, I would add a messagebox to display the entered values when the button was clicked. you may want to do this before and after an evaluation is performed; hrsWorked >= 41.

there's unnecessary code in the if else. to hint at it, i'll say some data just doesn't change.

to troubleshoot logic problems, drawing flowcharts is usually very effective. in my school, they had us create flowcharts before writing any code. what are the steps that need to be performed? Then write code for each action or evaluation. the data in the flowchart can be used as comments for your code.
 
Status
Not open for further replies.
Back
Top Bottom