473,748 Members | 6,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Switch() Statement Not Working

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>Untitl ed 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="JavaS cript">
function FuelCalculator( f)
{
var A;
A= Math.round((f.p pg.value/f.mpg.value)*10 0);
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="FuelCa lculator(this.f orm);" value="Calclula te"> </td>
</tr>
<td><input name="FSC" type="button" onClick="switch (b)"
value="FSC"></td>

</table>

</form>
</body>
</html>

Jan 31 '06 #1
19 3803
wrote on 31 jan 2006 in comp.lang.javas cript:
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)
Jan 31 '06 #2
Evertjan. wrote on 31 jan 2006 in comp.lang.javas cript:
wrote on 31 jan 2006 in comp.lang.javas cript:
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)
Jan 31 '06 #3

rd********@gmai l.com wrote:
<script language="JavaS cript">
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;

Jan 31 '06 #4
rd********@gmai l.com 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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Feb 1 '06 #5

<rd********@gma il.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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>Untitl ed 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="JavaS cript">
function FuelCalculator( f)
{
var A;
A= Math.round((f.p pg.value/f.mpg.value)*10 0);
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="FuelCa lculator(this.f orm);" value="Calclula te"> </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.
Feb 1 '06 #6
Hal Rosser wrote:
[Full quote]
<http://jibbering.com/faq/faq_notes/pots1.html#ps1P ost>
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
Feb 1 '06 #7
web.dev wrote:
rd********@gmai l.com 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..d efault instead, one could use the
// Interval prototype from my JSX:math.js instead; it provides
// a getIntervalInde x() 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="fuelCa lculator(this); ">');
</script>
PointedEars
Feb 1 '06 #8

Thomas 'PointedEars' Lahn wrote:
web.dev wrote:
rd********@gmai l.com 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.

Feb 1 '06 #9
web.dev wrote:
Thomas 'PointedEars' Lahn wrote:
web.dev wrote:
> rd********@gmai l.com 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
Feb 1 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
10908
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a switch-case has a variable (most probably an enumeration) & associated symbols or integral value. Selection is made, base on what symbol/value the variable holds. So
35
8349
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except for '5'. So in his switch statement, he omits a case for '5':
7
3377
by: Mark Hobley | last post by:
I am trying to rewrite a section of code within an existing program. This is a section of code (simplified and non-working) showing the structure that I am trying to achieve: char *ParseString (char *instring) { char *ptr1=instring if (instring == NULL)
2
1694
by: Cathleen C via DotNetMonster.com | last post by:
I'm a semi-beginner with c# and am having a problem effectively implementing a switch statement. I've created an asp.net app that runs a report depending on which item was selected from a drop down box from a previous page. I then have an Export button to convert the resultant report into pdf. I have it working but wanted to stream line the code. The oStream variable is set with the switch statement. After the switch I attempt to...
5
3926
by: _DS | last post by:
I'm currently using a switch with about 50 case statements in a stretch of code that's parsing XML attributes. Each case is a string. I'm told that switch statements will actually use hash tables when number of cases is around 10 or more, so I haven't changed the code. Just wondering if anyone knows about how that's structured internally. It does seem a bit slow.
12
12345
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
9
6281
by: Gordon | last post by:
I want to add a feature to a project I'm working on where i have multiple users set up on my Postgres database with varying levels of access. At the bare minimum there will be a login user who only has read access to the users table so that users can log in. Once a user has been logged in successfully I want to escalate that user's access level to one appropriate to their role, which will include switching the postgres user they are...
7
3360
by: Rohit | last post by:
Hi, I am working on a switch module which after reading voltage through a port pin and caterogizing it into three ranges(open,low or high), passes this range to a function switch_status() with parameters value and signal ID. Signal Id is used to get a user configurable parameter inside a configuration file, which depends on the type of switch. I have implemented it as under. Please ignore those magic numbers as I have mimized logic to...
13
11830
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so just curious to find out is it effective to use this method?? or is there is any other alternative method present so that execution time and code size can be reduced?? Thanks in advance.
0
8987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9534
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.