Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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>

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    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".

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,298
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    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?
    Last edited by Philip M; 12-05-2012 at 09:06 PM.

    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.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,543
    Thanks
    80
    Thanked 4,617 Times in 4,581 Posts
    ??? 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/>
    Last edited by Old Pedant; 12-05-2012 at 09:30 PM.
    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.

  • #5
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    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".


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •