Switch() Statement Not Working

R

rdavis7408

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>
 
E

Evertjan.

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


It is all in the name:

case is lowercase!

and give me a break:

you will have to use break;
 
L

Lee

(e-mail address 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
 
W

web.dev

<script language="JavaScript">

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

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;
 
L

Lasse Reichstein Nielsen

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
 
H

Hal Rosser

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.
 
T

Thomas 'PointedEars' Lahn

web.dev said:
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
 
T

Thomas 'PointedEars' Lahn

Hal said:
[Full quote]

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
 
W

web.dev

Thomas said:
web.dev said:
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.
 
T

Thomas 'PointedEars' Lahn

web.dev said:
Thomas said:
web.dev said:
(e-mail address 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?

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
 
J

John G Harris

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
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Wed, 1
Feb 2006 21:16:39 remote, seen in John G
Harris said:
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.
 
H

Hal Rosser

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.
 
R

Randy Webb

Hal Rosser said the following on 2/3/2006 2:58 AM:
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.
 
T

Thomas 'PointedEars' Lahn

me said:
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
 
T

Thomas 'PointedEars' Lahn

Sean said:
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.
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.
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.
LOL, on what basis do you make *that* assertion, exactly?

Personal experience, including experience of postings in this newsgroup
of much higher quality.
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
 
M

McKirahan

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".
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top