Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-05-2012, 07:06 PM   PM User | #1
EricaBrachman
New to the CF scene

 
Join Date: Oct 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
EricaBrachman is an unknown quantity at this point
Calling a function

Hi, I'm having some trouble with some code. What the page is supposed to be doing is: Prompting a user for their miles driven, prompting for their gallons used, and then it's supposed to call a function to divide the two and come out with their Miles Per Gallon.

Maybe I just need a fresh pair of eyes to see what I'm not, but I can't figure out my problem.

Code:
<html>
<head>
<script type="text/javascript">

// Program name: 
// Purpose: Get the users MPG
// Author: 
// Date last modified:


// Variables and constants
var miles_driven;               // the number of mile driven
var gallons_used;              // the number of gallons used
var miles_per_gallon;         // miles_driven / gallons_used
var ES = "";                     // literal empty string
var BR = "<br />";            // HTML line break

// function miles_per_gallon
 {
   miles_per_gallon = miles_driven / gallons_used;
     }
</script>
</head>
<body>
<script>



// Welcome the user, prompt for miles driven and gallons used
document.write("Welcome to the gas mileage calculator!" + BR);
miles_driven = prompt("Enter the number of miles driven",ES);
miles_driven = parseInt(miles_driven);
gallons_used = prompt("Enter the number of gallons used',ES);
gallons_used = parseInt(gallons_used);

// call miles_per_gallon function
miles_per_gallon();

alert("Your total MPG:" + miles_per_gallon);


</script>
</body>
</html>
EricaBrachman is offline   Reply With Quote
Old 12-05-2012, 07:25 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 950
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
The function isn't being called, it just does everything on page load. You should be getting error messages. The function doesn't even exist, really..

function miles_per_gallon(){

do stuff

}

window.onload = miles_per_gallon;// doesn't need the () for window.onload
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 12-05-2012, 08:01 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
miles_driven = parseInt(miles_driven);

felgall will say that you should use Math.floor to return an integer. parseInt() is really to change a number from one base to another. parseInt() is really supposed to be used to convert numbers from one base to another - e.g. binary to decimal, hexadecimal to decimal. Personally I don't see any real reason why parseInt() should not be used, but if you do you should include the radix (10).

Be aware that document.write() is in effect obsolete. document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

Have you tried using your error console?

Quizmaster: What was the currency of Holland before the Euro?
Contestant: Was it the Danish Krone?
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 12-05-2012 at 08:06 PM..
Philip M is offline   Reply With Quote
Old 12-05-2012, 08:09 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
??? She/He most certainly does *NOT* want to use
Code:
window.onload = miles_per_gallon;
That would be a huge mistake.

The real problem is that she/he has *BOTH* a FUNCTION and a VARIABLE with the *SAME NAME*: miles_per_gallone.

*** KABLOOEY ***

(Well, that would be true *IF* she/he actually had a function miles_per_gallon. The function declaration is both wrong and commented out.)

(And, finally, she/he has a string that starts with " and ends with ' which gives a syntax error.)

*************

Edit: Wolfshade's next comment duly noted. Probably gender put first. <grin/>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 12-05-2012 at 08:30 PM..
Old Pedant is offline   Reply With Quote
Old 12-05-2012, 08:21 PM   PM User | #5
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 950
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
I think OP is female, not male. Just sayin'.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 09:17 AM.


Advertisement
Log in to turn off these ads.