Help With A Calculation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Hagstrom

    Help With A Calculation

    Good Day -

    I have written a form which collects input for a fare calculation. The form
    collects the following:

    1) Departure or Destination Group (1,2,or 3)
    2) Number of Adults
    3) Number of Children aged 4 to 12
    4) Number of Children under age 4

    Based on this data, I need to calculate a fare as follows:

    1) The base fare is $80.00, $70.00 or $60.00 per person, depending on which
    Departure/Destination Group is selected. If there is more than one adult,
    all adults after the first one receive a 40% discount.

    2) The rate per Child 4 - 12 is one have the base fare per child.

    3) Children under 4 are free.

    The following is the form and the Javascript, but I cannot get it to work.

    Any help or suggestions would be truly appreciated.

    Mike Hagstrom

    *************** *************** *************** *************** *************** *
    *
    <form name="quote">
    <table width="500" border="2" cellspacing="2" cellpadding="2"
    align="CENTER">

    <tr><td>Selec t Your Group:&nbsp;</td><td>Key West Group&nbsp;<inp ut
    type="Radio" name="Group" value="1"><br>L ower Keys Group&nbsp;<inp ut
    type="Radio" name="Group" value="2"><br>M iddle/Upper Keys Group&nbsp;<inp ut
    type="Radio" name="Group" value="3"></td></tr>

    <tr><td>Numbe r of Adults:&nbsp;</td><td><INPUT NAME="Adults" SIZE=5
    MAXLENGTH=50></td></tr>

    <tr><td>Numbe r Children 4-12 yrs:&nbsp;</td><td><INPUT NAME="Chil412"
    SIZE=5 MAXLENGTH=50></td></tr>

    <tr><td>Numbe r Children Under 4 yrs:&nbsp;</td><td><INPUT NAME="ChilU4"
    SIZE=5 MAXLENGTH=50></td></tr>

    <tr><td ><input type=button value="Calculat e"
    onClick="calcul ate()"></td></tr>

    <tr><td>Total Passengers:</td><td><input type=text name=totalpass
    size=12></td></tr>

    <tr><td>Your Fare:</td><td><input type=text name=totalfare
    size=12></td></tr>


    </table>
    </form>
    <script type=text/javascript>
    function calculate() {
    if (document.quote .Group.value = 1)
    then Var farepp = 80;
    else if (document.quote .Group.value = 2)
    then Var farepp = 70;
    else Var farepp = 60;

    var Adults = document.quote. Adults.value;
    var Chil412 = document.quote. Chil412.value;
    var ChilU4 = document.quote. ChilU4.value;
    var totalpass = Math.pow(Adults +Chil412+ChiU4) ;
    var totalfare = Math.pow((farep p*Adults)+(Chil 412*farepp/2));


    document.quote. totalpass.value ="";
    document.quote. totalfare.value ="";

    }

    </script>


  • KC Wong

    #2
    Re: Help With A Calculation

    > I have written a form which collects input for a fare calculation. The
    form[color=blue]
    > collects the following:[/color]

    <snip assignment>
    [color=blue]
    > The following is the form and the Javascript, but I cannot get it to work.[/color]

    That's not good enough... next time state clearly:
    1. What results do you expect
    2. What incorrect results you're getting

    I'll give you some hints below.
    [color=blue]
    > <script type=text/javascript>
    > function calculate() {
    > if (document.quote .Group.value = 1)
    > then Var farepp = 80;
    > else if (document.quote .Group.value = 2)
    > then Var farepp = 70;
    > else Var farepp = 60;[/color]

    1. JavaScript does not have a "then" keyword.
    2. JavaScript, while loosely typed, are case-sensitive.
    3. Scope rules: What scope do you want fareapp to be in?
    [color=blue]
    > var Adults = document.quote. Adults.value;
    > var Chil412 = document.quote. Chil412.value;
    > var ChilU4 = document.quote. ChilU4.value;
    > var totalpass = Math.pow(Adults +Chil412+ChiU4) ;
    > var totalfare = Math.pow((farep p*Adults)+(Chil 412*farepp/2));[/color]

    There's a typo in one of the above lines... should be obvious when you see
    the error message.
    [color=blue]
    > document.quote. totalpass.value ="";
    > document.quote. totalfare.value ="";
    > }
    > </script>[/color]

    You want to display the results, right? Then you shouldn't assign empty
    strings to the fields here.

    BTW the calculation is incorrect too... results in NaN. I'll leave that part
    for you to fix... it looks like a "squish-the-bugs" kind of assignment to
    me.


    KC


    Comment

    • Michael Hagstrom

      #3
      Re: Help With A Calculation

      KC -

      Thanks for your input.

      Based on your comments, I edited the script to remove the conditionals on
      the "farepp" and set it static at 80. I've set the math to only calculate
      the full fare without a discount.

      I also cleaned up the typing and now I receive no error messages, but the
      script returns nothing.

      Here's the script as modified:
      *************** *************** *************** *
      <script type=text/javascript>
      function calculate() {

      var farepp = 80;
      var Adults = document.quote. Adults.value;
      var Chil412 = document.quote. Chil412.value;
      var ChilU4 = document.quote. ChilU4.value;
      var totalfare = Math.pow((farep p*Adults)+(fare pp*Chil412/2));
      var totalpass = Math.pow(Adults +Chil412+ChilU4 );

      if (!isNaN(totalfa re) &&
      (totalfare != Number.POSITIVE _INFINITY) &&
      (totalfare != Number.NEGATIVE _INFINITY)) {
      document.quote. totalfare.value =Math.round(tot alfare);
      document.quote. totalpass.value =Math.round(tot alpass);
      }
      else {
      document.quote. totalfare.value ="";
      document.quote. totalpass.value ="";
      }

      }
      function round(totalfare ) {return Math.round(tota lfare *100)/100;}
      </script>
      *************** *************** *************** *************** *************** *
      *
      Am I missing something obvious here?

      Thanks for any more help or suggestions.

      Mike

      "KC Wong" <sterilize.the. spammers@killki llkill.com> wrote in message
      news:bkrnp2$506 v1$1@ID-200690.news.uni-berlin.de...[color=blue][color=green]
      > > I have written a form which collects input for a fare calculation. The[/color]
      > form[color=green]
      > > collects the following:[/color]
      >
      > <snip assignment>
      >[color=green]
      > > The following is the form and the Javascript, but I cannot get it to[/color][/color]
      work.[color=blue]
      >
      > That's not good enough... next time state clearly:
      > 1. What results do you expect
      > 2. What incorrect results you're getting
      >
      > I'll give you some hints below.
      >[color=green]
      > > <script type=text/javascript>
      > > function calculate() {
      > > if (document.quote .Group.value = 1)
      > > then Var farepp = 80;
      > > else if (document.quote .Group.value = 2)
      > > then Var farepp = 70;
      > > else Var farepp = 60;[/color]
      >
      > 1. JavaScript does not have a "then" keyword.
      > 2. JavaScript, while loosely typed, are case-sensitive.
      > 3. Scope rules: What scope do you want fareapp to be in?
      >[color=green]
      > > var Adults = document.quote. Adults.value;
      > > var Chil412 = document.quote. Chil412.value;
      > > var ChilU4 = document.quote. ChilU4.value;
      > > var totalpass = Math.pow(Adults +Chil412+ChiU4) ;
      > > var totalfare = Math.pow((farep p*Adults)+(Chil 412*farepp/2));[/color]
      >
      > There's a typo in one of the above lines... should be obvious when you see
      > the error message.
      >[color=green]
      > > document.quote. totalpass.value ="";
      > > document.quote. totalfare.value ="";
      > > }
      > > </script>[/color]
      >
      > You want to display the results, right? Then you shouldn't assign empty
      > strings to the fields here.
      >
      > BTW the calculation is incorrect too... results in NaN. I'll leave that[/color]
      part[color=blue]
      > for you to fix... it looks like a "squish-the-bugs" kind of assignment to
      > me.
      >
      >
      > KC
      >
      >[/color]


      Comment

      • Dr John Stockton

        #4
        Re: Help With A Calculation

        JRS: In article <Laecb.22204$an .17894@bignews6 .bellsouth.net> , seen in
        news:comp.lang. javascript, Michael Hagstrom <hagstr_m@bells outh.net>
        posted at Wed, 24 Sep 2003 06:18:21 :-

        Before writing programs using maths, learn some math; before writing
        code using a system function, discover what it does.
        [color=blue]
        > var totalfare = Math.pow((farep p*Adults)+(fare pp*Chil412/2));
        > var totalpass = Math.pow(Adults +Chil412+ChilU4 );[/color]
        [color=blue]
        >function round(totalfare ) {return Math.round(tota lfare *100)/100;}[/color]

        That rounds a number to a number. You will want to round a number to a
        string with two decimals.
        [color=blue]
        >Am I missing something obvious here?[/color]

        Yes : the FAQ, including how to reply in News and other matters.

        [color=blue]
        >"KC Wong" <sterilize.the. spammers@killki llkill.com> wrote in message
        >news:bkrnp2$50 6v1$1@ID-200690.news.uni-berlin.de...[/color]


        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

        Comment

        • Michael Hagstrom

          #5
          Re: Help With A Calculation

          Thanks for all your input -

          I got the script working fine.

          Sorry about using the Math.pow system function - I had taken it from another
          piece of code I had written earlier, and forgot to remove it from my
          example.

          Thank you again.

          Oh, yes - my math is just fine, and my name is
          Dr. Michael Hagstrom - Catholic University of America, 1969



          "Dr John Stockton" <spam@merlyn.de mon.co.uk> wrote in message
          news:fF5rNUE+qd c$Ew2M@merlyn.d emon.co.uk...[color=blue]
          > JRS: In article <Laecb.22204$an .17894@bignews6 .bellsouth.net> , seen in
          > news:comp.lang. javascript, Michael Hagstrom <hagstr_m@bells outh.net>
          > posted at Wed, 24 Sep 2003 06:18:21 :-
          >
          > Before writing programs using maths, learn some math; before writing
          > code using a system function, discover what it does.
          >[color=green]
          > > var totalfare = Math.pow((farep p*Adults)+(fare pp*Chil412/2));
          > > var totalpass = Math.pow(Adults +Chil412+ChilU4 );[/color]
          >[color=green]
          > >function round(totalfare ) {return Math.round(tota lfare *100)/100;}[/color]
          >
          > That rounds a number to a number. You will want to round a number to a
          > string with two decimals.
          >[color=green]
          > >Am I missing something obvious here?[/color]
          >
          > Yes : the FAQ, including how to reply in News and other matters.
          >
          >[color=green]
          > >"KC Wong" <sterilize.the. spammers@killki llkill.com> wrote in message
          > >news:bkrnp2$50 6v1$1@ID-200690.news.uni-berlin.de...[/color]
          >
          >
          > --
          > © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE[/color]
          4 ©[color=blue]
          > <URL:http://jibbering.com/faq/> Jim Ley's FAQ for[/color]
          news:comp.lang. javascript[color=blue]
          > <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates,[/color]
          sources.[color=blue]
          > <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics,[/color]
          links.


          Comment

          • Dr John Stockton

            #6
            Re: Help With A Calculation

            JRS: In article <ql2db.27104$an .13869@bignews6 .bellsouth.net> , seen in
            news:comp.lang. javascript, Michael Hagstrom <hagstr_m@bells outh.net>
            posted at Fri, 26 Sep 2003 17:39:58 :-
            [color=blue]
            >Oh, yes - my math is just fine, and my name is
            >Dr. Michael Hagstrom - Catholic University of America, 1969[/color]

            You seem to be a slow learner. Read the newsgroup FAQ, and try to learn
            about the proper, considerate formatting of news replies. If that's not
            enough, see the references below.
            [color=blue]
            >"Dr John Stockton" <spam@merlyn.de mon.co.uk> wrote in message
            >news:fF5rNUE+q dc$Ew2M@merlyn. demon.co.uk...[color=green]
            >> JRS: In article <Laecb.22204$an .17894@bignews6 .bellsouth.net> , seen in
            >> news:comp.lang. javascript, Michael Hagstrom <hagstr_m@bells outh.net>
            >> posted at Wed, 24 Sep 2003 06:18:21 :-[/color][/color]
            [color=blue][color=green][color=darkred]
            >> >Am I missing something obvious here?[/color]
            >>
            >> Yes : the FAQ, including how to reply in News and other matters.[/color][/color]
            [color=blue][color=green]
            >> --
            >> © John
            >> ...[/color][/color]

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME ©
            Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
            Web <URL:http://www.merlyn.demo n.co.uk/news-use.htm> : about usage of News.
            No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

            Comment

            Working...