473,327 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

HELP! JavaScript Code Alert Problems

This is a program to just calculate a weekly gross salary based on the
hourly wage and hours worked (with overtime too). I am not getting the
alert box when it calculates it. Can anyone see what I might be doing
wrong? It is a smaller program so if someone could please help me
out!!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtmll/DTD/xhtmll-transitional.dtd">
<html>
<head>
<title>Project 10-1</title>
<script type="text/javascript">
<!--HIDE FROM INCOMPATABLE BROWSERS

function grossSalaryCalc() {
if (document.wages.hoursWorked.value 40) {
var overHours = hoursWorked -
40;
var overPay = overHours * 1.5 *
hourlyWage;
var grossPay = (hourlyWage * 40) +
overPay;
alert("Your weekly gross salary is " +
grossPay);
}
else {
var grossPay = hourlyWage * hoursWorked;
alert("Your weekly gross salary is " + grossPay);
}
}
//STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>
</head>
<body>
<form name="wages" action="" method="get">
<p><label for="hoursWorked">Hours Worked</label>&nbsp;<input
type="text" name="hoursWorked" id="hoursWorked" /></p>
<p><label for="hourlyWage">Hourly Wage</label>&nbsp;<input type="text"
name="hourlyWage" id="hourlyWage" /></p>
<p><button type="button" onclick="grossSalaryCalc()">Calculate Gross
Salary</button>
</p>
</form>
</body>
</html>

Mar 19 '07 #1
10 1797
MichiganMan said the following on 3/19/2007 7:16 PM:
This is a program to just calculate a weekly gross salary based on the
hourly wage and hours worked (with overtime too). I am not getting the
alert box when it calculates it. Can anyone see what I might be doing
wrong? It is a smaller program so if someone could please help me
out!!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtmll/DTD/xhtmll-transitional.dtd">
Why do people keep trying to give IE xHTML?
<html>
<head>
<title>Project 10-1</title>
Project or Homework Assignment?
<script type="text/javascript">
<!--HIDE FROM INCOMPATABLE BROWSERS
You can drop that nonsense.
function grossSalaryCalc() {
if (document.wages.hoursWorked.value 40) {
if (+document.wages.hoursWorked.value 40){

Don't depend on the browser to maybe convert when you can explicitly
cause it to happen.
var overHours = hoursWorked - 40;
"hoursWorked" is undefined.

Where did you define hoursWorked?
var overPay = overHours * 1.5 * hourlyWage;
hourlyWage is undefined.
var grossPay = (hourlyWage * 40) + overPay;
alert("Your weekly gross salary is " + grossPay);
}
else {
var grossPay = hourlyWage * hoursWorked;
Again, hourlyWage and hoursWorked are undefined.

"How do I get the value of a form control?"
<URL: http://jibbering.com/faq/index.html#FAQ4_13>

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 19 '07 #2
Thanks for the quick response. To declare a variable, I would just
put:
var hourlyWage
var hoursWorked

before the function and If Statement in the head section?

Mar 19 '07 #3
MichiganMan said the following on 3/19/2007 7:36 PM:
Thanks for the quick response.
Thanks for quoting what you are replying to next time.
To declare a variable, I would just put:
var hourlyWage
var hoursWorked
That would be one way. Personally, I would consult the URL I gave you
and set it something like this:

var hourlyWage = document.wages.hourlyWage.value;

And so on.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 19 '07 #4
On Mar 19, 7:36 pm, "MichiganMan" <WebL...@aol.comwrote:
Thanks for the quick response. To declare a variable, I would just
put:
var hourlyWage
var hoursWorked

before the function and If Statement in the head section?
Yes, but you still have to assign them values...I assume you expect
these two variables to use their
repectively named textfields...

My guess is you would use something like

function grossSalaryCalc() {
try {
var hoursWorked = +document.wages.hoursWorked.value;
var hourlyWage = +document.wages.hourlyWage.value;
if (hoursWorked 40) {
var overHours = hoursWorked - 40;
var overPay = overHours * 1.5 * hourlyWage;
var grossPay = (hourlyWage * 40) + overPay;
alert("Your weekly gross salary is " + grossPay);
}
else {
var grossPay = hourlyWage * hoursWorked;
alert("Your weekly gross salary is " + grossPay);
}
}
catch(e) {
alert("Please enter an hourly wage and hours worked.");
}
}

Mar 19 '07 #5
"MichiganMan" <We*****@aol.comwrote:
<snip>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtmll/DTD/xhtmll-transitional.dtd">
<html>
<head>
<title>Project 10-1</title>
<script type="text/javascript">
<!--HIDE FROM INCOMPATABLE BROWSERS
It is permissible for an XML parse (XHTML is XML) to strip comments as it
is parsing the mark-up, so they do not come out of the other end. If that
happened here then your script element would become empty.

In any event, how many browser that understand XHTML at all do not know
how to handle a script element (regardless of whether they support
scripting or not)? This "hiding form older browsers" stuff has been
redundant in HTML documents since the late 1990's, it certainly does not
need to appear in an XHTML document.
function grossSalaryCalc() {
if (document.wages.hoursWorked.value 40) {
<snip>
You have not stated which browser you are getting to execute this code,
so we know little of the specifics of the XHTML DOM it is creating for
the document. However, XHTML DOMs tend to follow the DOM specifications
more strictly than HTML DOMs, and they often do not reproduce the
'shortcuts' common in HTML DOMs. This includes the 'shortcut' of
referencing form controls as named properties of the document object, or
form controls as named properties of the form element. Instead it is
advisable to use the formally specified - document.forms - collection to
reference form elements, and the from element's - elements - collection
to reference from controls.

On the other hand, what you have here may be only the illusion of XHTML,
and so none of the above applies, but then why use XHTML-style mark-up in
that case?

Richard.

Mar 20 '07 #6
On Mar 19, 7:43 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
MichiganMan said the following on 3/19/2007 7:36 PM:
Thanks for the quick response.

Thanks for quoting what you are replying to next time.
To declare a variable, I would just put:
var hourlyWage
var hoursWorked

That would be one way. Personally, I would consult the URL I gave you
and set it something like this:

var hourlyWage = document.wages.hourlyWage.value;
I placed this line of code in along with the hoursWorked to declare
the variables, and now I am finaly getting the alert message. But it
says "Your weekly gross salary is NaN? where whatever NaN is should
have been replaced with the grossPay variable. Any ideas?

I really appreciate your help, been working on this all day and I am
getting hung up on the final touches.

>
And so on.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/

Mar 20 '07 #7
On Mar 19, 8:01 pm, "MichiganMan" <WebL...@aol.comwrote:
On Mar 19, 7:43 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
MichiganMan said the following on 3/19/2007 7:36 PM:
Thanks for the quick response.
Thanks for quoting what you are replying to next time.
To declare a variable, I would just put:
var hourlyWage
var hoursWorked
That would be one way. Personally, I would consult the URL I gave you
and set it something like this:
var hourlyWage = document.wages.hourlyWage.value;

I placed this line of code in along with the hoursWorked to declare
the variables, and now I am finaly getting the alert message. But it
says "Your weekly gross salary is NaN? where whatever NaN is should
have been replaced with the grossPay variable. Any ideas?

I see now it means "Not a Number", but why isn't it seeing the
grossPay as a calculated number?

I really appreciate your help, been working on this all day and I am
getting hung up on the final touches.
>

And so on.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/- Hide quoted text -

- Show quoted text -

Mar 20 '07 #8
On Mar 20, 10:25 am, "MichiganMan" <WebL...@aol.comwrote:
On Mar 19, 8:01 pm, "MichiganMan" <WebL...@aol.comwrote:
On Mar 19, 7:43 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
MichiganMan said the following on 3/19/2007 7:36 PM:
Thanks for the quick response.
Thanks for quoting what you are replying to next time.
To declare a variable, I would just put:
var hourlyWage
var hoursWorked
That would be one way. Personally, I would consult the URL I gave you
and set it something like this:
var hourlyWage = document.wages.hourlyWage.value;
I placed this line of code in along with the hoursWorked to declare
the variables, and now I am finaly getting the alert message. But it
says "Your weekly gross salary is NaN? where whatever NaN is should
have been replaced with the grossPay variable. Any ideas?

I see now it means "Not a Number", but why isn't it seeing the
grossPay as a calculated number?
Somewhere in your undisclosed code you are getting a result that is
not a number, usually by trying to do an arithmetic operation with
something that can't be type converted to a number.

The usual debugging trick is to use an alert to show the value of a
variable of interest. Insert it at a point where you are sure your
code is working, then move it down (or up) until the error is
revealed. Anyhow, try:

function grossSalaryCalc() {
var grossPay = 0;
var overPay = 0;
var f = document.forms['wages'];
var hoursWorked = +f.elements['hoursWorked'].value;
var hourlyWage = +f.elements['hourlyWage'].value;

if (hoursWorked 40) {
overPay = (hoursWorked - 40) * 1.5 * hourlyWage;
hoursWorked = 40;
}

grossPay = hoursWorked * hourlyWage + overPay;
alert("Your weekly gross salary is " + grossPay);
}
The work you have left to do is:

- validate the input is suitable, presumably it should be only
valid decimal numbers, e.g. 12.5.
<URL:
http://groups.google.com.au/group/co...3269a1a18a24cf
>
- format your output as currency, e.g. $0.00 - see the FAQ:
<URL: http://www.jibbering.com/faq/#FAQ4_6 >
--
Rob

Mar 20 '07 #9
On Mar 19, 7:01 pm, "MichiganMan" <WebL...@aol.comwrote:
On Mar 19, 7:43 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
MichiganMan said the following on 3/19/2007 7:36 PM:
Thanks for the quick response.
Thanks for quoting what you are replying to next time.
To declare a variable, I would just put:
var hourlyWage
var hoursWorked
That would be one way. Personally, I would consult the URL I gave you
and set it something like this:
var hourlyWage = document.wages.hourlyWage.value;
try var hourlyWage = +document.wages.hourlyWage.value;

The + ensures that a conversion to a Number is happening and that you
don't try to do math on a String. You can also use
parseInt(document.wages.hourlyWage.value) or
parseFloat(document.wages.hourlyWage.value) if that suites you.
>
I placed this line of code in along with the hoursWorked to declare
the variables, and now I am finaly getting the alert message. But it
says "Your weekly gross salary is NaN? where whatever NaN is should
have been replaced with the grossPay variable. Any ideas?

I really appreciate your help, been working on this all day and I am
getting hung up on the final touches.
And so on.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/

Mar 20 '07 #10
Tom Cole said the following on 3/20/2007 9:53 AM:
On Mar 19, 7:01 pm, "MichiganMan" <WebL...@aol.comwrote:
>On Mar 19, 7:43 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
>>MichiganMan said the following on 3/19/2007 7:36 PM:
Thanks for the quick response.
Thanks for quoting what you are replying to next time.
To declare a variable, I would just put:
var hourlyWage
var hoursWorked
That would be one way. Personally, I would consult the URL I gave you
and set it something like this:
var hourlyWage = document.wages.hourlyWage.value;

try var hourlyWage = +document.wages.hourlyWage.value;

The + ensures that a conversion to a Number is happening and that you
don't try to do math on a String. You can also use
parseInt(document.wages.hourlyWage.value) or
parseFloat(document.wages.hourlyWage.value) if that suites you.
That isn't the problem though. And the conversion to Numeric will happen
automatically when the next line multiplies the String value by a Number.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 20 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
4
by: Sarir Khamsi | last post by:
Is there a way to get help the way you get it from the Python interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the module cmd.Cmd? I know how to add commands and help text to...
2
by: Sudheer Kareem | last post by:
Dear All Please tell me how to assosiate help files with my Vb.net Project. Regards Sudheer
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
0
by: tbatwork828 | last post by:
If you were like me trying to figure out how to launch context sensitive help topic by the context id, here is the link: http://weblogs.asp.net/kencox/archive/2004/09/12/228349.aspx and if...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
2
by: BT Openworld | last post by:
I have just had to upgrade to Access 2003 as Access 97 EMail (SendObject) doesn't work when loaded on Windows XP. I'm finding my way around Access 2003 but my biggest problem is getting...
9
by: JJ | last post by:
Do you all use HTML help workshop to create your help system. I am finding it quite clumsy to use. Mayeb because I am not used to using it. Do any of you use any other techniques to create help...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.