Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Switch() Statement Not Working

Reply
Thread Tools

Switch() Statement Not Working

 
 
rdavis7408@gmail.com
Guest
Posts: n/a
 
      01-31-2006
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>

 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      01-31-2006
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)
 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      01-31-2006
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)
 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      01-31-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
web.dev
Guest
Posts: n/a
 
      01-31-2006

(E-Mail Removed) 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;

 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      02-01-2006
(E-Mail Removed) 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 - (E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Hal Rosser
Guest
Posts: n/a
 
      02-01-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.


 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-01-2006
web.dev wrote:

> (E-Mail Removed) 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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-01-2006
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
 
Reply With Quote
 
web.dev
Guest
Posts: n/a
 
      02-01-2006

Thomas 'PointedEars' Lahn wrote:
> web.dev wrote:
>
> > (E-Mail Removed) 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if statement that, when false, skips first statement in its block, executes second? Jay McGavren Java 11 01-16-2006 05:49 PM
How do I do a conditional statement in a constant statement? tkvhdl@gmail.com VHDL 3 12-16-2005 06:13 PM
Which of switch statement and if-else statement takes less time to execute? swaroophr@gmail.com C Programming 21 08-02-2005 09:24 AM
exec "statement" VS. exec "statement in globals(), locals() Ted Python 1 07-22-2004 08:51 AM
exec "statement" VS. exec "statement" in globals(), locals() tedsuzman Python 2 07-21-2004 08:41 PM



Advertisments