Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,366 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,296 people online right now. Registration is fast and FREE... Join Now!




Uncooperative Function

 
Reply to this topicStart new topic

Uncooperative Function

raeNet
post 30 Sep, 2008 - 05:51 AM
Post #1


D.I.C Head

**
Joined: 1 Nov, 2007
Posts: 98


My Contributions


QUOTE

I'm having one heck of a time trying to get this seemingly basic script to work. *User enters beginning miles, ending miles and gallons used to calculate mpg. These elements use onchange events to invoke the calcMPG function.

I haven't even attempted a nested if stmt. within the else clause to verify value of gallons entered >0, if value is not >0, the stmt. within the if stmt. shouldn't execute.

Can anyon help??


CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Calculate MPG</title>
<meta http-equiv="content-type" content="text/html;
     charset=iso-8859-1" />
<script type="text/javascript">
/* <![CDATA[ */
function calcMPG() {
    var startMiles = parseInt(document.mpgform.startingMileage.value);
    var endMiles = parseInt(document.mpgform.endingMileage.value);
    var gallons = parseInt(document.mpgform.gallonsUsed.value);
    var mpg = ((mpgform.endingMileage - mpgform.startingMileage)/mpgform.gallonsUsed);
}

    if (isNaN(startingMileage.value) && isNaN(endingMileage.value) && isNaN(gallonsUsed.value)){
    window.alert("You must enter a number");
}
    else{
    document.mpgform.milesPerGallon.value = mpg;
}

/* ]]> */
</script>
</head>
<body>
<form name="mpgform" action="">
[color=#FF0000]<label>Starting mileage:</label>[/color]    <input type="text" name="startingMileage" value="0" onchange="calcMPG();"><br />
[color=#FF0000]<label>Ending mileage:</label>[/color]    <input type="text" name="endingMileage" value="0" onchange="calcMPG();"><br />
[color=#FF0000]<label>Gallons used:</label>[/color]
    <input type="text" name="gallonsUsed" value="0" onchange="calcMPG();"><br />
<label>Miles per gallon:</label>
    <input type="text" name="milesPerGallon" value="0";><br />
</form>
</body>
</html>


This post has been edited by raeNet: 30 Sep, 2008 - 06:26 AM
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 30 Sep, 2008 - 07:54 AM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,027



Thanked 173 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Your if statement and such should be IN your function and not outside it. Plus you had some variable names that were not defined. Make sure you are using the correct variables that you had setup in the function. Here is what it could look like...

jscript

<script type="text/javascript">
/* <![CDATA[ */
function calcMPG() {

var startMiles = parseInt(document.mpgform.startingMileage.value);
var endMiles = parseInt(document.mpgform.endingMileage.value);
var gallons = parseInt(document.mpgform.gallonsUsed.value);

// Check if startmiles, endmiles and gallons are all greater than zero
if ((startMiles > 0) && (endMiles > 0) && (gallons > 0)) {
// Calculate mpg (used absolute value in case endMiles is less than startMiles... otherwise you can check and prevent if endmiles is less than start miles)
var mpg = (Math.abs(endMiles - startMiles)/gallons);
document.mpgform.milesPerGallon.value = mpg;
}
}



/* ]]> */
</script>


Keep in mind that the onchange event will fire when you enter your value and SWITCH to another field. If you are wanting to calculate just by typing, you will need to look into the onkeypress, onkeydown, and onkeyup events.

Enjoy!

"At DIC we be mileage calculating code ninjas....we all have miles to go to becoming programming grand masters of the universe!" decap.gif
User is offlineProfile CardPM

Go to the top of the page

raeNet
post 30 Sep, 2008 - 08:13 AM
Post #3


D.I.C Head

**
Joined: 1 Nov, 2007
Posts: 98


My Contributions


Thanks so much for your quick reply. One problem though, I need to use an if...else stmt. using isNaN for first three input boxes, which I did in my original code, but then to prevent division by zero with gallons input box only, I need to nest an if stmt within the else clause to insure value >0.
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 30 Sep, 2008 - 08:19 AM
Post #4


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,027



Thanked 173 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


well then, you got some work to do. My code provides half of what you need. I have the if statement for checking if they are greater than zero, you can put your isNaN tests in there as well as tack on the else statement.

You do have to do a little work yourself you know. wink2.gif
User is offlineProfile CardPM

Go to the top of the page

xerxes333
post 30 Sep, 2008 - 08:21 AM
Post #5


D.I.C Head

Group Icon
Joined: 5 Jul, 2007
Posts: 146



Thanked 7 times

Dream Kudos: 25
My Contributions


jscript

<script type="text/javascript">
/* <![CDATA[ */
function calcMPG() {
var startMiles = parseInt(document.mpgform.startingMileage.value);
var endMiles = parseInt(document.mpgform.endingMileage.value);
var gallons = parseInt(document.mpgform.gallonsUsed.value);
if(gallons!=0){
var mpg = ((endMiles - startMiles)/gallons);
document.mpgform.milesPerGallon.value = mpg;
}
else{
document.mpgform.milesPerGallon.value = document.mpgform.milesPerGallon.value;
}
}
/* ]]> */
</script>


hmmmm apparently I post to slow rolleyes.gif
User is offlineProfile CardPM

Go to the top of the page

raeNet
post 30 Sep, 2008 - 08:41 AM
Post #6


D.I.C Head

**
Joined: 1 Nov, 2007
Posts: 98


My Contributions


QUOTE
Well I've tried to nest the if in the if...else and now I'm getting (3) Errors: Object expected on lines 33,36,39 (lines that begin with label???)
Any suggestions?

CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Calculate Gas Mileage</title>
<meta http-equiv="content-type" content="text/html;
     charset=iso-8859-1" />
<script type="text/javascript">
/* <![CDATA[ */
function calcMPG() {
    var startMiles = parseInt(document.mpgform.startingMileage.value);
    var endMiles = parseInt(document.mpgform.endingMileage.value);
    var gallons = parseInt(document.mpgform.gallonsUsed.value);

    // use isNAN fx within compound conditional stmt. to determine whether input boxes contain numbers
    if (isNaN(startingMileage) && isNaN(endingMileage) && isNaN(gallonsUsed)){
    window.alert("You must enter a number");

    // use nested if stmt. to verify gallons input value is >0
        if (gallons >0){
        var mpg=((endMiles - startMiles)/gallons);
}
    else{
    document.mpgform.milesPerGallon.value = document.mpgform.milesPerGallon.value;
}
}

/* ]]> */
</script>
</head>
<body>
<form name="mpgform" action="">
<label>Starting mileage:
    <input type="text" name="startingMileage" value="0" onchange="calcMPG();"><br />
</label>
<label>Ending mileage:
    <input type="text" name="endingMileage" value="0" onchange="calcMPG();"><br />
</label>
<label>Gallons used:
    <input type="text" name="gallonsUsed" value="0" onchange="calcMPG();"><br />
</label>
<label>Miles per gallon:</label>
    <input type="text" name="milesPerGallon" value="0";><br />
</form>
</body>
</html>
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 30 Sep, 2008 - 09:33 AM
Post #7


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,027



Thanked 173 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Remember if they are not a number (that is isNaN returns true) you DON'T want to be doing the calculation. Your if statement is saying that if all the elements are not numbers then do your calculation. Also the line document.mpgform.milesPerGallon.value = document.mpgform.milesPerGallon.value; is doing nothing since you are just taking the value and give it back to itself.

Don't try and rush this, read through the logic and see if it makes sense. smile.gif

User is offlineProfile CardPM

Go to the top of the page

raeNet
post 30 Sep, 2008 - 09:34 AM
Post #8


D.I.C Head

**
Joined: 1 Nov, 2007
Posts: 98


My Contributions


QUOTE
Well, I've tried changing my if/if/else stmts., which seems to flow better, but now the object error is at lines 29,31,33, which begin form name,input,input.

I'm not sure where to go from here??

CODE

<script type="text/javascript">
/* <![CDATA[ */
function calcMPG() {
    var startMiles = parseInt(document.mpgform.startingMileage.value);
    var endMiles = parseInt(document.mpgform.endingMileage.value);
    var gallons = parseInt(document.mpgform.gallonsUsed.value);
    var mpg = ((endMiles - startMiles)/gallons);

    // use isNAN fx within compound conditional stmt. to determine whether input boxes contain numbers
    if (isNaN(startMiles) && isNaN(endMiles) && isNaN(gallons)){
        if (gallons <0)
        window.alert("You must enter a number");
        document.mpgform.milesPerGallon.value = document.mpgform.milesPerGallon.value;
            else
            document.mpgform.milesPerGallon.value = mpg;
}
/* ]]> */
</script>
</head>
<body>
<form name="mpgform" action="">
<label>Starting mileage:</label>
    <input type="text" name="startingMileage" value="0" onchange="calcMPG();"><br />
<label>Ending mileage:</label>
    <input type="text" name="endingMileage" value="0" onchange="calcMPG();"><br />
<label>Gallons used:</label>
    <input type="text" name="gallonsUsed" value="0" onchange="calcMPG();"><br />
<label>Miles per gallon:</label>
    <input type="text" name="milesPerGallon" value="0";><br />
</form>
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 30 Sep, 2008 - 09:51 AM
Post #9


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,027



Thanked 173 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


You can't do this... var mpg = ((endMiles - startMiles)/gallons); BEFORE you test the values of the variables. You cannot do calculations before you have tested endMiles, startMiles, gallons to see if they are valid. This part will be in the if statement for if the values are valid.

User is offlineProfile CardPM

Go to the top of the page

raeNet
post 30 Sep, 2008 - 10:20 AM
Post #10


D.I.C Head

**
Joined: 1 Nov, 2007
Posts: 98


My Contributions


QUOTE
I've moved the calculation, which makes sense - thank you. But, I just don't understand the relentless "object expected errors" beginning at the form line when inputing numbers.
What are they referring to? I've checked for typos. Any suggestions?

CODE

function calcMPG() {
    var startMiles = parseInt(document.mpgform.startingMileage.value);
    var endMiles = parseInt(document.mpgform.endingMileage.value);
    var gallons = parseInt(document.mpgform.gallonsUsed.value);
// use isNAN fx within compound conditional stmt. to determine whether input boxes contain numbers
    if ((isNaN(startMiles) && isNaN(endiMles) && isNaN(gallons))){
        if (gallons <0)
        alert("You must enter a number");
            else
            var mpg = ((endMiles - startMiles)/gallons);
            document.mpgform.milesPerGallon.value = mpg;
}
/* ]]> */
</script>
</head>
<body>
<form name="mpgform" action="">
<p><label for="startingMileage">Starting mileage:</label></p><input type="text"


This post has been edited by raeNet: 30 Sep, 2008 - 10:20 AM
User is offlineProfile CardPM

Go to the top of the page

raeNet
post 30 Sep, 2008 - 10:59 AM
Post #11


D.I.C Head

**
Joined: 1 Nov, 2007
Posts: 98


My Contributions


QUOTE
I've gotten rid of the object expected errors. I removed the label coding on the text boxes. Now, the form code is:

CODE

<form name="mpgform" action="">
<p>Starting mileage:
<input type="text" name="startingMileage" value="0" onchange="calcMPG();"><br /></p>
<p>Ending mileage:
<input type="text" name="endingMileage" value="0" onchange="calcMPG();"><br /></p>
<p>Gallons used:
<input type="text" name="gallonsUsed" value="0" onchange="calcMPG();"><br /></p>
<p>Miles per gallon:
<input type="text" name="milesPerGallon" value="0";></p>
</form>


QUOTE
But, now I'm back to an unresponsive input screen. I tried tracing errors with "window.alert() Method", but all checked out. I'm open for suggestions, if anyone has any... thx.
User is offlineProfile CardPM

Go to the top of the page

xerxes333
post 1 Oct, 2008 - 08:24 AM
Post #12


D.I.C Head

Group Icon
Joined: 5 Jul, 2007
Posts: 146



Thanked 7 times

Dream Kudos: 25
My Contributions


This should get it going.
jscript
function calcMPG() {
var startMiles = parseInt(document.mpgform.startingMileage.value);
var endMiles = parseInt(document.mpgform.endingMileage.value);
var gallons = parseInt(document.mpgform.gallonsUsed.value);
// use isNAN fx within compound conditional stmt. to determine whether input boxes contain numbers

if (!(isNaN(startMiles) && isNaN(endMiles) && isNaN(gallons))){
if (gallons==0){
alert("You must enter a number");
}
else{
var mpg = ((endMiles - startMiles)/gallons);
document.mpgform.milesPerGallon.value = mpg;
}
}
}
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 05:09AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month