Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Javascript - syntax or logic error (or both) ?
Closed Thread
Old 03-07-2009, 08:59 AM   #1 (permalink)
oldskool's Avatar
 
Electrical Systems Design

Join Date: Jun 2008

Location: Maine, USA

Posts: 1,646

oldskool has a spectacular aura aboutoldskool has a spectacular aura about

Default Javascript - syntax or logic error (or both) ?

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;
    }

__________________
  • Operating system(s) : Windows 7 Home Premium x64 | Ubuntu 9.10
  • CPU : Pentium Dual-Core E5200 @ 2.50GHz /Socket 775 LGA
  • Memory: 2X 2GB DDR2 SDRAM
  • Hard drive: 640 GB Western Digital Caviar Blue 7200rpm WD6400AAKS-75
  • Motherboard : Dell 0U880P
  • Video: nVidia GeGorce 8400GS
  • Chipset : Intel G41

<<<<< If you found anything I said helpful, please click the or under my avatar
oldskool is offline  
Old 03-07-2009, 01:03 PM   #2 (permalink)
office politics's Avatar
 
It's all just 1s and 0s

Join Date: Jan 2004

Location: in the lab

Posts: 4,410

office politics will become famous soon enough

Default Re: Javascript - syntax or logic error (or both) ?

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.
office politics is offline  
Old 03-07-2009, 02:16 PM   #3 (permalink)
 

Join Date: Jul 2005

Location: England

Posts: 2,161

kmote has a spectacular aura aboutkmote has a spectacular aura about

Default Re: Javascript - syntax or logic error (or both) ?

I assume it's pay * 1 for hours 1-40 then pay * 1.5 from there on.

extraHrsWorked = hrsWorked - 40;
hrsWorked = hrsWorked - extraHrsWorked;
pay = (payRate * hrsWorked) + ((1.5 * payRate) + extraHrsWorked);
__________________
MSI P43 Neo|Enermax Pro82+ 425W|E5200|silent 8500GT|250GB Samsung spinpoint F1|Samsung SATA DVD RW|4GB Corsair|Antec SOLO|openSUSE11


There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page
kmote is offline  
Old 03-08-2009, 05:01 AM   #4 (permalink)
oldskool's Avatar
 
Electrical Systems Design

Join Date: Jun 2008

Location: Maine, USA

Posts: 1,646

oldskool has a spectacular aura aboutoldskool has a spectacular aura about

Default Re: Javascript - syntax or logic error (or both) ?

Thank you, guys ! Very much appreciated
__________________
  • Operating system(s) : Windows 7 Home Premium x64 | Ubuntu 9.10
  • CPU : Pentium Dual-Core E5200 @ 2.50GHz /Socket 775 LGA
  • Memory: 2X 2GB DDR2 SDRAM
  • Hard drive: 640 GB Western Digital Caviar Blue 7200rpm WD6400AAKS-75
  • Motherboard : Dell 0U880P
  • Video: nVidia GeGorce 8400GS
  • Chipset : Intel G41

<<<<< If you found anything I said helpful, please click the or under my avatar
oldskool is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript code & tutorials oldskool Programming Discussions 4 02-01-2009 05:20 PM
Using javascript to hide and unhide elements dynamically Osiris Programming Discussions 0 01-14-2009 08:56 PM
Firefox 3.1 beta arrives with JavaScript booster turned off Osiris Browser & General Internet Questions 3 10-15-2008 03:50 PM
The A-Z of Programming Languages: JavaScript Osiris Programming Discussions 0 08-04-2008 09:35 AM
PHP: How to modify a downloaded JavaScript code and partly change the code within it? LincolnX Programming Discussions 4 01-25-2008 04:25 PM