JavaScript NaN Code Problem

M

MichiganMan

This was from a previous post, but I wanted to clean it up a bit and
repost with a final question. This is just a simple program that will
calculate a weekly salary by what is entered into the hours worked and
hourly wage text fields.

My problem is when I click to see the alert with the caculated amount,
it says "Your weekly gross salary is NaN" where NaN is supposed to be
"grossPay". I am using IE if that helps?

Any ideas? I appreciate it!

<!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
var hoursWorked
var hourlyWage
function grossSalaryCalc() {
if (document.wages.hoursWorked.value > 40) {
var overHours = hoursWorked -
40;
var overPay = overHours * 1.5 *
hourlyWage;
var grossPay = overPay + (hourlyWage *
40) ;
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>
 
R

RobG

This was from a previous post, but I wanted to clean it up a bit and
repost with a final question. This is just a simple program that will
calculate a weekly salary by what is entered into the hours worked and
hourly wage text fields.

Please manually wrap code at about 70 characters to prevent auto-
wrapping, which nearly alwasy introduces more errors.

My problem is when I click to see the alert with the caculated amount,
it says "Your weekly gross salary is NaN" where NaN is supposed to be
"grossPay". I am using IE if that helps?

In this case, it makes no difference.
Any ideas? I appreciate it!

See below.
<!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

As Randy said, get rid of that, it is a complete waste of time and as
Richard said, in a document that is treated as real XHTML browsers are
within their rights to completely ignore anything between <!-- and --
, i.e. your script will effectively be removed.

var hoursWorked

Here you declare hoursWorked but don't give it a value, so it has the
value 'undefined'. I think you meant to do:

var hoursWorked = document.wages.hoursWorked.value;


See my reply to your original thread.

var hourlyWage
function grossSalaryCalc() {
if (document.wages.hoursWorked.value > 40) {
var overHours = hoursWorked -
40;

undefined * 40 evaluates to NaN, which is assigned to overHours.

var overPay = overHours * 1.5 *
hourlyWage;

overHours is NaN, anything * NaN evaluates to NaN so now overPay is
also NaN.

var grossPay = overPay + (hourlyWage *
40) ;

Not only is hourlyWage not defined, overPay is NaN so your sum is:

var grossPay = NaN + (undefined * 40);

and now grossPay is NaN too. You *must* assign a value to variables
before using them for arithmetic - see my reply to your original
thread.

alert("Your weekly gross salary is " +
grossPay);}


The expected result is: "Your weekly gross salary is NaN" so your
program works perfectly, it just doesn't give the result you
expected. :)
 
M

MichiganMan

I declared the variables and gave them values using
var hoursWorked = document.wages.hoursWorked.value;
var hourlywage = document.wages.hourlyWage.value;
before the function section.
This should have created a value, instead of undefined * 40 = NaN

It still doesn't work. I am thinking I left a bracket out or something
at this point? It has to be something I am just not seeing.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
oglegroups.com>, Mon, 19 Mar 2007 21:47:42, MichiganMan
<!--HIDE FROM INCOMPATABLE BROWSERS
YSCIB.


var hourlyWage
//STOP HIDING FROM INCOMPATIBLE BROWSERS -->
!YSCIB

<input type="text"
name="hourlyWage" id="hourlyWage" />

There is no connection between the var and the name or ID, except for
the spelling. The following is abbreviated and tested, and shows what
you need to do; test it by putting 1e3 as the input.


<form name="wages">
<input type=text name=XXX>
<input type=button onClick="ZZZ()">
</form>

<script>
function ZZZ() {
var YYY = +document.wages.XXX.value
alert(YYY) }
</script>

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,224
Latest member
BettieToom

Latest Threads

Top