View Full Version : Calling a function

12-05-2012, 08:06 PM
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.

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

// 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

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


12-05-2012, 08:25 PM
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

Philip M
12-05-2012, 09:01 PM
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?

Old Pedant
12-05-2012, 09:09 PM
??? She/He most certainly does *NOT* want to use

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/>

12-05-2012, 09:21 PM
I think OP is female, not male. Just sayin'.