1. Attachments are working again! Check out this thread for more details and to report any other bugs.

Seeking some JavaScript help

Discussion in 'Fred's House of Pancakes' started by TonyPSchaefer, Jan 15, 2008.

  1. TonyPSchaefer

    TonyPSchaefer Your Friendly Moderator
    Staff Member

    Joined:
    May 11, 2004
    14,816
    2,498
    66
    Location:
    Far-North Chicagoland
    Vehicle:
    2017 Prius Prime
    Model:
    Prime Advanced
    I started with some "borrowed" code and have made an online mileage comparison calculator. The thing is, I can't do math. Well, I can but the calculator can't. Some of the fields work as desired, others do not.

    The calculator is here: Mileage Comparison Calculator

    I'm hoping this is an easy thing. That is to say, I'm hoping that it's just that I don't understand how to do math in JavaScript. I'm hoping it doesn't turn into several lines of shorthand script I can't read much less understand.

    So if anyone wants to take a look at the source code and tell me where my math is all wrong, I'd greatly appreciate it.
     
  2. eagle33199

    eagle33199 Platinum Member

    Joined:
    Mar 2, 2006
    5,122
    268
    0
    Location:
    Minnesota
    Vehicle:
    2015 Prius v wagon
    Model:
    Two
    Tony, i took a look and found the problem. How do you go about calculating gallons per year given miles/gallon and miles/year? I'll give you a hint, it's not multiplication!

    Change the last 4 lines of math there to division (citymiles/city + highwaymiles/highway, i believe is what you want), and i think you'll be a lot closer to what you want :)
     
  3. eagle33199

    eagle33199 Platinum Member

    Joined:
    Mar 2, 2006
    5,122
    268
    0
    Location:
    Minnesota
    Vehicle:
    2015 Prius v wagon
    Model:
    Two
    PS. the top section seems to calculate everything correctly.
     
  4. Danny Hamilton

    Danny Hamilton Active Member

    Joined:
    Apr 30, 2007
    926
    94
    0
    Location:
    Greater Chicagoland Area
    Vehicle:
    2007 Prius
    Model:
    N/A
    Try these 2 changes:

    form.yourcargallons.value =
    (((1.0 * form.citymiles.value) / (1.0 * form.city.value)) +
    ((1.0 * form.highwaymiles.value) / (1.0 * form.highway.value)));

    form.priusgallons.value = (((1.0 * form.citymiles.value) / 60.0 ) +
    ((1.0 * form.highwaymiles.value) / 51.0));

    And you might want to add these four lines just before the return:

    <!-- Round to 2 decimal points -->
    form.yourcargallons.value = (Math.round(form.yourcargallons.value * 100.0) / 100.0);
    form.yourcarcost.value = (Math.round(form.yourcarcost.value * 100.0) / 100.0);
    form.priuscargallons.value = (Math.round(form.priuscargallons.value * 100.0) / 100.0);
    form.priuscost.value = (Math.round(form.priuscost.value * 100.0) / 100.0);
     
  5. TonyPSchaefer

    TonyPSchaefer Your Friendly Moderator
    Staff Member

    Joined:
    May 11, 2004
    14,816
    2,498
    66
    Location:
    Far-North Chicagoland
    Vehicle:
    2017 Prius Prime
    Model:
    Prime Advanced
    Dude.
    You guys rock!
    I've uploaded the updated version.


    You know that saying, "never enough"?

    Once I got it working (okay, once you got it working), my next goal was going to be formatting.
    For example, the cost fields should have two decimal places and the gallons can have one or two. These are estimates, of course and I even question the need for decimals in estimates. And is there a way to comma-group the larger numbers?
     
  6. Danny Hamilton

    Danny Hamilton Active Member

    Joined:
    Apr 30, 2007
    926
    94
    0
    Location:
    Greater Chicagoland Area
    Vehicle:
    2007 Prius
    Model:
    N/A
    I couldn't remember if there was a method in javascript for that, however a quick search at Google found this:

    Formatting numbers in JavaScript - mredkj.com

    I haven't tried it yet, so I won't vouch for how well it works.
     
  7. Danny Hamilton

    Danny Hamilton Active Member

    Joined:
    Apr 30, 2007
    926
    94
    0
    Location:
    Greater Chicagoland Area
    Vehicle:
    2007 Prius
    Model:
    N/A
    That same site recommends using num.toFixed() for rounding rather than multiplying by a factor of ten then rounding and then dividing by the same factor of ten. That seems more elegant than what I posted earlier.
     
  8. boulder_bum

    boulder_bum Senior Member

    Joined:
    Mar 11, 2007
    1,371
    38
    0
    Location:
    Castle Rock, CO
    Vehicle:
    2007 Prius
    I think one of the reasons why it's a little difficult to debug the problem is because the javascript, in its current form is a little hard to read. Hopefully the below would not only solve your immediate issue, but also make the script easier to understand and maintain. Of note is that I'm using getElementById() insted of form.controlName because it's a little more resiliant (you no longer need to know about any "form" and your code won't break if you move, add or rename forms). This requires that an id attribute be added to all the controls, though (which only takes a minute).

    If you want to make the utility awesome (and because I'm in the mood to give unsolicited advice), it would be even better if you added these features to the interface:

    1. Instead of having the user enter in their MPG rating (which requires them to look it up), have them enter in the year/make/model of their car. You can do this with cascading drop downs and AJAX (which is easy with ASP.NET if you want to play around with it). You can download all of the data from: Download the Fuel Economy Database then store it locally for use in your system.

    2. Default the annual miles driven to the national average 12,000 miles.

    3. Use drop down lists with increments of 10% for percentage city/highway driving. Default to 50%/50% and change the other drop down list's selection based on the selection of its peer so it always equals 100%. If you're adventurous, use sliders and do the same.

    4. Automatically lookup the average price of gas daily/weekly and default to that.