Switch() Statement Not Working

Discussion in 'Javascript' started by rdavis7408@gmail.com, Jan 31, 2006.

  1. Guest

    Hello, I have four textboxes that the user enters the price per gallon
    paid at the pump, the mileage per gallon and I would like to then
    calculate the cost per gallon and use a switch statement to pull a
    value based on the price per gallon.

    For example if the price of fuel is 2.44 per gallon and the enter that
    they get 5.9 miles per gallon the cost of that mile is $.41. Then based
    on the cost per gallon of 2.44 we might pay them another $.20 per
    gallon based off of the numbers in the switch statement.

    The calculation worked until I added the switch statement. Why is that?
    Can anyone help?

    Thanks ahead of time. Below is the script:

    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html;
    charset=iso-8859-1">
    <link href="fsc.css" rel="stylesheet" type="text/css">
    </head>

    <body>

    <script language="JavaScript">
    function FuelCalculator(f)
    {
    var A;
    A= Math.round((f.ppg.value/f.mpg.value)*100);
    f.cpg.value = "$"+(A/100);
    }

    var b;
    b=this.form.ppg.value
    switch(b)
    {
    Case >=1.25 && <=1.30:
    this.form.dlnt.value=.01;
    Case >=1.31 && <=1.369:
    this.form.dlnt.value=.02;
    Case >=1.37 && <=1.429:
    this.form.dlnt.value=.03;
    default:
    this.form.dlnt.value=.99

    }



    </script>

    <form action="">

    <table width="351">
    <tr>
    <td width="213"> Cents per gallon:</td>
    <td width="60" > <input name="ppg" type="text"
    style="text-align:center" value="0" size="10">
    </td>
    </tr>
    <tr>
    <td>Miles per gallon:</td>
    <td> <input name="mpg" type="text" value="0" size="10"
    style="text-align:center">
    </td>
    </tr>
    <tr>
    <td>Fuel Cost Per Mile: </td>
    <td > <input name="cpg" type= "text" disabled="true" value="0"
    size="10" style="text-align:center">
    </td>
    <tr>
    <td>Dillon Fuel Surcharge Payment: </td>
    <td> <input name="dlnt" type="text" disabled="true" value="0"
    size="10" style="text-align:center">
    </td>
    <tr>
    <td colspan="2" ><input name="button" type="button"
    onClick="FuelCalculator(this.form);" value="Calclulate"> </td>
    </tr>
    <td><input name="FSC" type="button" onClick="switch(b)"
    value="FSC"></td>

    </table>

    </form>
    </body>


    </html>
     
    , Jan 31, 2006
    #1
    1. Advertising

  2. Evertjan. Guest

    wrote on 31 jan 2006 in comp.lang.javascript:

    > switch(b)
    > {
    > Case >=1.25 && <=1.30:
    > this.form.dlnt.value=.01;
    >


    It is all in the name:

    case is lowercase!

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Jan 31, 2006
    #2
    1. Advertising

  3. Evertjan. Guest

    Evertjan. wrote on 31 jan 2006 in comp.lang.javascript:

    > wrote on 31 jan 2006 in comp.lang.javascript:
    >
    >> switch(b)
    >> {
    >> Case >=1.25 && <=1.30:
    >> this.form.dlnt.value=.01;
    >>

    >
    > It is all in the name:
    >
    > case is lowercase!
    >


    and give me a break:

    you will have to use break;

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Jan 31, 2006
    #3
  4. Lee Guest

    said:
    >
    >Hello, I have four textboxes that the user enters the price per gallon
    >paid at the pump, the mileage per gallon and I would like to then
    >calculate the cost per gallon and use a switch statement to pull a
    >value based on the price per gallon.
    >
    >For example if the price of fuel is 2.44 per gallon and the enter that
    >they get 5.9 miles per gallon the cost of that mile is $.41. Then based
    >on the cost per gallon of 2.44 we might pay them another $.20 per
    >gallon based off of the numbers in the switch statement.
    >
    >The calculation worked until I added the switch statement. Why is that?


    Because you apparently guessed that the switch statement in Javascript
    works like the switch statement in some other language that you've
    used. It compares a single value to a list of values, not to a list
    of comparison expressions. Your syntax is wrong, too:

    http://docs.sun.com/source/816-6408-10/stmt.htm#1018610
     
    Lee, Jan 31, 2006
    #4
  5. web.dev Guest

    wrote:
    > <script language="JavaScript">


    The language attribute is deprecated, use the type attribute instead:

    <script type = "text/javascript">

    > b=this.form.ppg.value


    This statement is error-prone. Instead give your form a name and
    access its elements in the following fashion:

    var b = document.forms["formName"].elements["ppg"].value;

    > switch(b)
    > {
    > Case >=1.25 && <=1.30:
    > this.form.dlnt.value=.01;
    > Case >=1.31 && <=1.369:
    > this.form.dlnt.value=.02;
    > Case >=1.37 && <=1.429:
    > this.form.dlnt.value=.03;
    > default:
    > this.form.dlnt.value=.99
    >
    > }


    1. It is not 'Case', but should be 'case' with a lowercase C.
    2. You should have a 'break' after one or more cases. And a 'break'
    after the default case.
    3. The switch construct does not deal with a range of values. You
    should instead use if...else statements instead.

    var dintVal = .99;

    if(b >= 1.25 && b <= 1.3)
    {
    dintVal = .01
    }
    else if(b >= 1.31 && b <= 1.369)
    {
    dintVal = .02
    }
    else if(b >= 1.37 && b <= 1.429)
    {
    dintVal = .03;
    }

    document.forms["formName"].elements["dInt"].value = dintVal;
     
    web.dev, Jan 31, 2006
    #5
  6. writes:

    > The calculation worked until I added the switch statement. Why is that?


    > switch(b)
    > {
    > Case >=1.25 && <=1.30:
    > this.form.dlnt.value=.01;
    > Case >=1.31 && <=1.369:
    > this.form.dlnt.value=.02;
    > Case >=1.37 && <=1.429:
    > this.form.dlnt.value=.03;
    > default:
    > this.form.dlnt.value=.99
    >
    > }


    You are guessing blindly. That rarely works in programming, since
    computers are notoriously bad at guessing what you really meant.

    The syntax of a case statement is:
    case <expression>: <statement>

    Example:

    switch(num){
    case 1: // something for 1
    break;
    case 2: case 3: // something for 2 or 3
    break;
    default: // ...
    }

    In your case, you want to match intervals, not values, so
    a switch statement isn't the immediate choice. Instead
    use a sequence of if-statements:

    if (b >= 1.25 && b <= 1.30) {
    this.form.dlnt.value=.01;
    } else if (b > 1.30 && b < 1.37) {
    ...
    } else if (b >= 1.37 && b < 1.43) {
    ...
    } else {
    ...
    }


    You can get the same effect with a switch, but it's not really
    smarter, and it's much less readable.

    switch(true) {
    case (b >= 1.25 && b <= 1.30) :
    this.form.dlnt.value=.01;
    break;
    case (b > 1.30 && b < 1.37) :
    ...
    break;
    ...
    }

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Feb 1, 2006
    #6
  7. Hal Rosser Guest

    <> wrote in message
    news:...
    > Hello, I have four textboxes that the user enters the price per gallon
    > paid at the pump, the mileage per gallon and I would like to then
    > calculate the cost per gallon and use a switch statement to pull a
    > value based on the price per gallon.
    >
    > For example if the price of fuel is 2.44 per gallon and the enter that
    > they get 5.9 miles per gallon the cost of that mile is $.41. Then based
    > on the cost per gallon of 2.44 we might pay them another $.20 per
    > gallon based off of the numbers in the switch statement.
    >
    > The calculation worked until I added the switch statement. Why is that?
    > Can anyone help?
    >
    > Thanks ahead of time. Below is the script:
    >
    > <html>
    > <head>
    > <title>Untitled Document</title>
    > <meta http-equiv="Content-Type" content="text/html;
    > charset=iso-8859-1">
    > <link href="fsc.css" rel="stylesheet" type="text/css">
    > </head>
    >
    > <body>
    >
    > <script language="JavaScript">
    > function FuelCalculator(f)
    > {
    > var A;
    > A= Math.round((f.ppg.value/f.mpg.value)*100);
    > f.cpg.value = "$"+(A/100);
    > }
    >
    > var b;
    > b=this.form.ppg.value
    > switch(b)
    > {
    > Case >=1.25 && <=1.30:
    > this.form.dlnt.value=.01;
    > Case >=1.31 && <=1.369:
    > this.form.dlnt.value=.02;
    > Case >=1.37 && <=1.429:
    > this.form.dlnt.value=.03;
    > default:
    > this.form.dlnt.value=.99
    >
    > }
    >
    >
    >
    > </script>
    >
    > <form action="">
    >
    > <table width="351">
    > <tr>
    > <td width="213"> Cents per gallon:</td>
    > <td width="60" > <input name="ppg" type="text"
    > style="text-align:center" value="0" size="10">
    > </td>
    > </tr>
    > <tr>
    > <td>Miles per gallon:</td>
    > <td> <input name="mpg" type="text" value="0" size="10"
    > style="text-align:center">
    > </td>
    > </tr>
    > <tr>
    > <td>Fuel Cost Per Mile: </td>
    > <td > <input name="cpg" type= "text" disabled="true" value="0"
    > size="10" style="text-align:center">
    > </td>
    > <tr>
    > <td>Dillon Fuel Surcharge Payment: </td>
    > <td> <input name="dlnt" type="text" disabled="true" value="0"
    > size="10" style="text-align:center">
    > </td>
    > <tr>
    > <td colspan="2" ><input name="button" type="button"
    > onClick="FuelCalculator(this.form);" value="Calclulate"> </td>
    > </tr>
    > <td><input name="FSC" type="button" onClick="switch(b)"
    > value="FSC"></td>
    >
    > </table>
    >
    > </form>
    > </body>
    >
    >
    > </html>


    It appears you are mixing VB's "Select case" syntax with Javascript's
    "switch" syntax, and the result is you missed'em both. Easy mistake - take
    a look at
    http://devguru.com for a reference on both VB and Javascript.
     
    Hal Rosser, Feb 1, 2006
    #7
  8. web.dev wrote:

    > wrote:
    >> b=this.form.ppg.value

    >
    > This statement is error-prone.


    Nonsense.

    > Instead give your form a name and access its elements in the following
    > fashion:
    >
    > var b = document.forms["formName"].elements["ppg"].value;


    No, the approach using the `form' property was the better one, however it
    only works if used in the right place.

    <meta http-equiv="Content-Script-Type" content="text/javascript">
    ...
    <script type="text/javascript">
    function fuelCalculator(o)
    {
    var es;
    if (o && o.form && (es = o.form.elements))
    {
    var b = +es['ppg'].value;
    var A = Math.round((b / es['mpg'].value) * 100);
    es['cpg'].value = "$" + (A / 100);

    var t = es['dlnt'];

    // To use switch..case..default instead, one could use the
    // Interval prototype from my JSX:math.js instead; it provides
    // a getIntervalIndex() method which returns the numeric array
    // index of the interval that contains a value.

    if (b >= 1.25 && b <= 1.30)
    {
    t.value = 0.01;
    }
    else if (b >= 1.31 && b <= 1.369)
    {
    t.value = 0.02;
    }
    else if (b >= 1.37 && b <= 1.429)
    {
    t.value= 0.03;
    }
    else
    {
    t.value= 0.99;
    }
    }
    }

    document.write('<input type="button" name="btn1"'
    + ' onclick="fuelCalculator(this);">');
    </script>


    PointedEars
     
    Thomas 'PointedEars' Lahn, Feb 1, 2006
    #8
  9. Hal Rosser wrote:

    > [Full quote]


    <http://jibbering.com/faq/faq_notes/pots1.html#ps1Post>

    > It appears you are mixing VB's "Select case" syntax with Javascript's
    > "switch" syntax, and the result is you missed'em both. Easy mistake -
    > take a look at
    > http://devguru.com for a reference on both VB and Javascript.


    devguru.com contains a lot of factually incorrect and obsolete
    information about JS/ECMAScript and is therefore not recommended.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Feb 1, 2006
    #9
  10. web.dev Guest

    Thomas 'PointedEars' Lahn wrote:
    > web.dev wrote:
    >
    > > wrote:
    > >> b=this.form.ppg.value

    > >
    > > This statement is error-prone.

    >
    > Nonsense.
    >
    > > Instead give your form a name and access its elements in the following
    > > fashion:
    > >
    > > var b = document.forms["formName"].elements["ppg"].value;

    >
    > No, the approach using the `form' property was the better one, however it
    > only works if used in the right place.


    In his script, doesn't "this" reference the global window object?
    Isn't that why it's error-prone? Otherwise, I do agree that
    "this.form" would've been better if used in the right place.
     
    web.dev, Feb 1, 2006
    #10
  11. web.dev wrote:

    > Thomas 'PointedEars' Lahn wrote:
    >> web.dev wrote:
    >> > wrote:
    >> >> b=this.form.ppg.value
    >> >
    >> > This statement is error-prone.

    >>
    >> Nonsense.
    >>
    >> > Instead give your form a name and access its elements in the following
    >> > fashion:
    >> >
    >> > var b = document.forms["formName"].elements["ppg"].value;

    >>
    >> No, the approach using the `form' property was the better one, however it
    >> only works if used in the right place.

    >
    > In his script, doesn't "this" reference the global window object?


    It probably refers to the Global Object there. That is not necessarily a
    Window object or the same object the global `window' property refers to.

    > Isn't that why it's error-prone?


    AFAIK, it is an error in the context used by the OP, not only error-prone.
    Or can you name any UA where it would work with the OP's HTML code?

    > Otherwise, I do agree that "this.form" would've been better if used in the
    > right place.


    ACK


    PointedEars
     
    Thomas 'PointedEars' Lahn, Feb 1, 2006
    #11
  12. In article <>, Thomas 'PointedEars'
    Lahn <> writes

    <snip>
    > if (b >= 1.25 && b <= 1.30)
    > {
    > t.value = 0.01;
    > }
    > else if (b >= 1.31 && b <= 1.369)
    > {
    > t.value = 0.02;
    > }
    > else if (b >= 1.37 && b <= 1.429)
    > {
    > t.value= 0.03;
    > }
    > else
    > {
    > t.value= 0.99;
    > }

    <snip>

    It would be better if there was only one number in each if condition.
    There is then less risk of typing errors and it should be easier to
    proof-read.

    if (b < 1.25)
    t.value = 0.99 // illegal value

    else if (b <= 1.3)
    t.value = 0.01 // interval 0

    else if (b < 1.31)
    t.value = 0.99 // illegal value, gap between intervals

    else if (b <= 1.369)
    t.value = 0.02 // interval 1

    else if (b < 1.37)
    t.value = 0.99 // illegal value, gap between intervals

    else if (b <= 1.429)
    t.value = 0.03 // interval 2

    else
    t.value = 0.99 // illegal value

    I'm also very suspicious of those gaps between the intervals.

    John
    --
    John Harris
     
    John G Harris, Feb 1, 2006
    #12
  13. JRS: In article <>, dated Wed, 1
    Feb 2006 21:16:39 remote, seen in news:comp.lang.javascript, John G
    Harris <> posted :
    >In article <>, Thomas 'PointedEars'
    >Lahn <> writes
    >
    > <snip>


    >I'm also very suspicious of those gaps between the intervals.


    Agreed. It must have been past Ears' bedtime, since otherwise he should
    have spotted that.

    There's a more important potential problem.

    The numbers in the conditions, numbers such as 1.37, are literals and
    will take whatever value of a floating-point binary IEEE Double is
    deemed nearest. The only two-place decimal fractions represented
    exactly are .00 .25 .50 .75.

    In this case, the quantities being compared with those may well
    themselves be effectively two-place literals rather than arithmetically-
    calculated values; but that will not necessarily always hold.
    Arithmetically-calculated values are subject to rounding error, unless
    everything can be held exactly in a Double. However, the OP's "rules"
    are evidently written with exact two-place decimals in mind.

    The OP's code should very probably be modified to work in terms of cents
    rather than dollars; integers up to 2^53 are held exactly.

    Writing it as a critical value table, using a well-tested lookup
    function, might well make it more maintainable.

    Using a decimal point without a digit on each side is deprecated, as
    liable to lead to transcription error.

    <FAQENTRY> ISTM that the FAQ does not address the desirability of
    working in integers whenever practicable, such as with currency; and
    that it should.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Feb 2, 2006
    #13
  14. Hal Rosser Guest

    > devguru.com contains a lot of factually incorrect and obsolete
    > information about JS/ECMAScript and is therefore not recommended.
    >
    >
    > PointedEars


    Is there a good web site ?
    Devguru may have errors, but its better than no reference at all.
     
    Hal Rosser, Feb 3, 2006
    #14
  15. Randy Webb Guest

    Hal Rosser said the following on 2/3/2006 2:58 AM:
    >> devguru.com contains a lot of factually incorrect and obsolete
    >> information about JS/ECMAScript and is therefore not recommended.
    >>
    >>
    >> PointedEars

    >
    > Is there a good web site ?


    http://jibbering.com/faq and related links therein.

    > Devguru may have errors, but its better than no reference at all.


    Not true. Bad sites are worse than no site. Once you find a good site,
    you have to re-learn it all after you wasted your time learning bad
    habits/code.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Feb 3, 2006
    #15
  16. me wrote:

    > Others have commented on a few of the misinterpretations in the code. All
    > logical guesses, but not close enough :)


    You are talking nonsense.

    > The best advice I can give anyone using JS is to go for the excellent
    > O'Reilly reference:
    >
    > http://www.oreilly.com/catalog/jscript4/index.html


    This is recommended in the FAQ only because all other books are worse.

    > and you could do worse than have the DHTML reference to had as well:
    >
    > http://www.oreilly.com/catalog/dhtmlref2/index.html


    You could do worse, but this is bad enough. A book published four years ago
    is certainly obsolete. The examples in the example chapter prove that.

    > Superb references if you want more than a lightweight quick-and-dirty
    > understanding of each.


    Wrong.

    BTW: Are you Kamal Sharma of Nugen.net, owner of mememe.com, or do you have
    his explicit permission to use his second-level domain? If not, you better
    stop this domain abuse at once.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Feb 5, 2006
    #16
  17. Sean Inglis wrote:

    > On Sun, 05 Feb 2006 19:20:40 +0100, Thomas 'PointedEars' Lahn wrote:
    >> me wrote:
    >>> Others have commented on a few of the misinterpretations in the code.
    >>> All logical guesses, but not close enough :)

    >> You are talking nonsense.

    >
    > Perhaps I should clear something up right off the bat; the "not close
    > enough" remark was made in response to the guesses made in the OPs
    > original code, not the subsequent responses, most of which hit the nail on
    > the head.


    ACK

    > Perhaps this acknowledgement puts you in a better temper?


    It is not my temper that is the matter here. But JFYI, as you are so
    concerned with my health, I have been quite calm when I was writing that
    and I am now.

    >>> The best advice I can give anyone using JS is to go for the excellent
    >>> O'Reilly reference:
    >>>
    >>> http://www.oreilly.com/catalog/jscript4/index.html

    >> This is recommended in the FAQ only because all other books are worse.

    >
    > Who's FAQ?


    The FAQ of this newsgroup.

    > Why should I care?


    Because you are not a fool?

    > And in any case, why does this have relevance?


    Because it is the FAQ of _this_ newsgroup, written by and contributed to by
    its accepted regulars, people whose knowledge about and understanding of
    the subjects discussed probably exceeds yours by orders of magnitude?

    > I recommend it to the OP from my personal experience (and the

    ^^^^^^^
    > response of others I recommend it to).

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    Pardon?

    > Is your own personal experience of this book different?


    Unfortunately, it is. Enough bad examples from it have been posted here.

    >>> and you could do worse than have the DHTML reference to had as well:
    >>>
    >>> http://www.oreilly.com/catalog/dhtmlref2/index.html

    >> You could do worse, but this is bad enough. A book published four years
    >> ago is certainly obsolete. The examples in the example chapter prove
    >> that.

    >
    > Ah, so you haven't read that one *either*?


    Of course not. Why should I read or even buy a book that is obviously
    obsolete and uses so many bad examples in just one chapter? Would you?

    > Once again, I am aware of the age of the book, and have had a steady
    > supply of others to compare it to. It is still without peer if you want to
    > *grok* DHTML/JS in a platform neutral manner, rather than firefight a
    > problem or hack something together.


    That there is nothing better (which I seriously doubt) does not make it
    any good.

    >>> Superb references if you want more than a lightweight
    >>> quick-and-dirty understanding of each.

    >> Wrong.

    >
    > LOL, on what basis do you make *that* assertion, exactly?


    Personal experience, including experience of postings in this newsgroup
    of much higher quality.

    >> BTW: Are you Kamal Sharma of Nugen.net, owner of mememe.com, or do you
    >> have his explicit permission to use his second-level domain? If not,
    >> you better stop this domain abuse at once.

    >
    > You've Googled!


    I have not, whois(1) sufficed. For some of us, the Internet is more than
    the Web.

    > How sweet.


    I am glad I could amuse you. You are easily impressed, yes?

    > But it's a fair point, I keep forgetting to reset my profile.
    >
    > I'd much prefer to use my real name to tell you to piss off, should it
    > come to that.


    I am sorry to say that your vocabulary does not form
    a non-empty intersecting set with mine. [psf 2.13]

    I do not care much about your real name, at least not in this NetNews
    top-level hierarchy. I care about (misguided) network abuse, and you
    should care about that, too.

    > Happy now?


    That was never the point. However, your From header is still not
    acceptable as there is no .house top-level domain (yet). See also
    <URL:http://giganews.com/legal/aup.html>


    HTH

    PointedEars
     
    Thomas 'PointedEars' Lahn, Feb 5, 2006
    #17
  18. McKirahan Guest

    <> wrote in message
    news:...
    > Hello, I have four textboxes that the user enters the price per gallon
    > paid at the pump, the mileage per gallon and I would like to then
    > calculate the cost per gallon and use a switch statement to pull a
    > value based on the price per gallon.


    You never clarified what the difference was between
    "price per gallon" and "cost per gallon"
    in your earlier post under
    "Simple Calculation in Form - 3 textboxes - 1 function".
     
    McKirahan, Feb 6, 2006
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. _
    Replies:
    1
    Views:
    426
    Enrique
    Feb 19, 2004
  2. Bruce Sam
    Replies:
    2
    Views:
    4,162
    Andrew McDonagh
    Jan 10, 2005
  3. Replies:
    21
    Views:
    1,071
    Giannis Papadopoulos
    Aug 2, 2005
  4. bthumber
    Replies:
    5
    Views:
    428
    Alexey Smirnov
    Jan 29, 2009
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    119
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page