So I have some code like:
if (document.Insur ance.State.sele ctedIndex == 1)
{
ifIll();
}
else if (document.Insur ance.State.sele ctedIndex == 2)
{
elseKan();
}
else if (document.Insur ance.State.sele ctedIndex == 3)
{
elseInd();
}
I am trying to replace the if-else statements with case statement as
follows:
var index = document.Insura nce.State.selec tedIndex;
switch (index)
{
case 1:
ifIll()
break
case 2:
elseKan()
break
case 3: elseInd()
break
}
This code doesn't work ! Am I missing something here? Thanks 17 2656
On Apr 16, 4:41 pm, "Navodit" <kaush...@uiuc. eduwrote:
So I have some code like:
if (document.Insur ance.State.sele ctedIndex == 1)
{
ifIll();}
else if (document.Insur ance.State.sele ctedIndex == 2)
{
elseKan();}
else if (document.Insur ance.State.sele ctedIndex == 3)
{
elseInd();
}
I am trying to replace the if-else statements with case statement as
follows:
var index = document.Insura nce.State.selec tedIndex;
switch (index)
{
case 1:
ifIll()
break
case 2:
elseKan()
break
case 3: elseInd()
break
}
This code doesn't work ! Am I missing something here? Thanks
I can't see the problem myself. I would recommend you either get a
good debugger (Firebug for Firefox is awesome), or that you add a
default: case to catch anything that falls off the end of the case,
which can alert as to what value was passed to the switch case, if
any. Also, it might be useful to use some try/catch clauses in there
somewhere, that way you will be notified of any errors.
All the best.
Daz.
On Apr 16, 4:41 pm, "Navodit" <kaush...@uiuc. eduwrote:
So I have some code like:
if (document.Insur ance.State.sele ctedIndex == 1)
{
ifIll();}
else if (document.Insur ance.State.sele ctedIndex == 2)
{
elseKan();}
else if (document.Insur ance.State.sele ctedIndex == 3)
{
elseInd();
}
I am trying to replace the if-else statements with case statement as
follows:
var index = document.Insura nce.State.selec tedIndex;
switch (index)
{
case 1:
ifIll()
break
case 2:
elseKan()
break
case 3: elseInd()
break
}
This code doesn't work ! Am I missing something here? Thanks
Oh, and another thing you should be aware of, is the type of the
argument you are passing to the switch case. You could be passing a
string instead of an integer, in which case you switch case should
look like this:
switch (index) {
case "1":
ifIll();
break;
case "2":
elseKan();
break;
case "3":
elseInd();
break;
}
NOT this:
switch (index) {
case 1:
ifIll();
break;
case 2:
elseKan();
break;
case 3:
elseInd();
break;
}
In either case (no pun intended), you should use a default case to
catch anything that drops through the case, unless you specifically
want them to be ignored.
Navodit wrote on 16 apr 2007 in comp.lang.javas cript:
So I have some code like:
if (document.Insur ance.State.sele ctedIndex == 1)
{
ifIll(); }
else if (document.Insur ance.State.sele ctedIndex == 2)
{
elseKan(); }
else if (document.Insur ance.State.sele ctedIndex == 3)
{
elseInd(); }
I am trying to replace the if-else statements with case statement as
follows:
var index = document.Insura nce.State.selec tedIndex;
switch (index)
{
case 1:
ifIll()
break
case 2:
elseKan()
break
case 3: elseInd()
break }
This code doesn't work ! Am I missing something here? Thanks
Probably you did, as this works fine:
=============== =============== ==========
<script type='text/javascript'>
function ch(x){
switch (x.selectedInde x) {
case 1:
alert('one');
break;
case 2:
alert('two');
break;
case 3:
alert('three');
break;
};
};
</script>
<select onchange='ch(th is)'>
<option--
<option1
<option2
<option3
</select>
=============== =============== ==========
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Navodit said:
>I am trying to replace the if-else statements with case statement as follows:
var index = document.Insura nce.State.selec tedIndex;
switch (index) {
case 1:
ifIll()
break
case 2:
elseKan()
break
case 3: elseInd()
break }
This code doesn't work ! Am I missing something here? Thanks
What does "doesn't work" mean? Are you seeing error messages?
Is something unexpected happening?
Those are really lousy choices for function names, by the way,
and are you sure you really need a separate function for each
state?
--
On Apr 16, 5:21 pm, Lee <REM0VElbspamt. ..@cox.netwrote :
Navodit said:
I am trying to replace the if-else statements with case statement as
follows:
var index = document.Insura nce.State.selec tedIndex;
switch (index)
{
case 1:
ifIll()
break
case 2:
elseKan()
break
case 3: elseInd()
break
}
This code doesn't work ! Am I missing something here? Thanks
What does "doesn't work" mean? Are you seeing error messages?
Is something unexpected happening?
Those are really lousy choices for function names, by the way,
and are you sure you really need a separate function for each
state?
--
In the OP's defense, I believe he's just trying to reverse engineer
the "if" statements, and he's testing it at each phase. To me, that's
a sensible approach, and I am sure that once the switch case does what
he's expecting it to do, he will no doubt change the function names.
Also, depending on the size of the functions needed to process the
data, I usually prefer keeping functions separate, as I find it's a
lot easier to organise code. I guess preferences differ...
"Daz" <cu********@gma il.comwrote in message
news:11******** **************@ b75g2000hsg.goo glegroups.com.. .
On Apr 16, 5:21 pm, Lee <REM0VElbspamt. ..@cox.netwrote :
>Navodit said:
>I am trying to replace the if-else statements with case statement as follows:
>var index = document.Insura nce.State.selec tedIndex;
>switch (index) {
case 1:
ifIll()
break
case 2:
elseKan()
break
case 3: elseInd()
break }
>This code doesn't work ! Am I missing something here? Thanks
What does "doesn't work" mean? Are you seeing error messages? Is something unexpected happening? Those are really lousy choices for function names, by the way, and are you sure you really need a separate function for each state?
--
In the OP's defense, I believe he's just trying to reverse engineer
the "if" statements, and he's testing it at each phase. To me, that's
a sensible approach, and I am sure that once the switch case does what
he's expecting it to do, he will no doubt change the function names.
Also, depending on the size of the functions needed to process the
data, I usually prefer keeping functions separate, as I find it's a
lot easier to organise code. I guess preferences differ...
Thanks for all the feedback and comments. First of all I have not written
those function names, my job is first to clean up the code as much as
possible and then think about renaming. Each of those functions corresponds
to around 50-100 lines of code so it is indeed useful to have them as
separate functions. Each of those functions populates dropdown with counties
for that particular state so when i say it doesnt work that implies the
dropdown doesn't get populated when I use the case statement while it does
get populated if the if statements are used.
So essentially this works:
if (document.Insur ance.State.sele ctedIndex == 1)
{
ifIll();
}
else if (document.Insur ance.State.sele ctedIndex == 2)
{
elseKan();
}
else if (document.Insur ance.State.sele ctedIndex == 3)
{
elseInd();
}
else if (document.Insur ance.State.sele ctedIndex == 4)
{
elseIw();
}
else if (document.Insur ance.State.sele ctedIndex == 6)
{
elseMich();
}
else if (document.Insur ance.State.sele ctedIndex == 7)
{
elseMin();
}
else if (document.Insur ance.State.sele ctedIndex == 8)
{
elseMis();
}
else if (document.Insur ance.State.sele ctedIndex == 9)
{
elseNeb();
}
else if (document.Insur ance.State.sele ctedIndex == 10)
{
elseND();
}
else if (document.Insur ance.State.sele ctedIndex == 11)
{
elseOh();
}
else if (document.Insur ance.State.sele ctedIndex == 12)
{
elseSD();
}
else if (document.Insur ance.State.sele ctedIndex == 13)
{
elseWI();
}
else if (document.Insur ance.State.sele ctedIndex == 5)
{
elseMar();
}
and this doesn't:
switch (document.Insur ance.State.sele ctedIndex)
{
case 1:
ifIll();
break;
case 2:
elseKan();
break;
case 3:
elseInd();
break;
case 4:
elseIw();
break;
case 5:
elseMar();
break;
casce 6:
elseMich();
break;
case 7:
elseMin();
break;
case 8:
elseMis();
break;
case 9:
elseNeb();
break;
case 10:
elseND();
break;
case 11:
elseOh();
break;
case 12:
elseSD();
break;
case 13:
elseWI();
break;
default:
break;
}
I also tried : case "1" etc and case '1' but that too doesnt work. Any
further ideas? Thanks
On Apr 16, 5:39 pm, "Navodit" <kaush...@uiuc. eduwrote:
"Daz" <cutenfu...@gma il.comwrote in message
news:11******** **************@ b75g2000hsg.goo glegroups.com.. .
On Apr 16, 5:21 pm, Lee <REM0VElbspamt. ..@cox.netwrote :
Navodit said:
I am trying to replace the if-else statements with case statement as
follows:
var index = document.Insura nce.State.selec tedIndex;
switch (index)
{
case 1:
ifIll()
break
case 2:
elseKan()
break
case 3: elseInd()
break
}
This code doesn't work ! Am I missing something here? Thanks
What does "doesn't work" mean? Are you seeing error messages?
Is something unexpected happening?
Those are really lousy choices for function names, by the way,
and are you sure you really need a separate function for each
state?
--
In the OP's defense, I believe he's just trying to reverse engineer
the "if" statements, and he's testing it at each phase. To me, that's
a sensible approach, and I am sure that once the switch case does what
he's expecting it to do, he will no doubt change the function names.
Also, depending on the size of the functions needed to process the
data, I usually prefer keeping functions separate, as I find it's a
lot easier to organise code. I guess preferences differ...
Thanks for all the feedback and comments. First of all I have not written
those function names, my job is first to clean up the code as much as
possible and then think about renaming. Each of those functions corresponds
to around 50-100 lines of code so it is indeed useful to have them as
separate functions. Each of those functions populates dropdown with counties
for that particular state so when i say it doesnt work that implies the
dropdown doesn't get populated when I use the case statement while it does
get populated if the if statements are used.
So essentially this works:
if (document.Insur ance.State.sele ctedIndex == 1)
{
ifIll();}
else if (document.Insur ance.State.sele ctedIndex == 2)
{
elseKan();}
else if (document.Insur ance.State.sele ctedIndex == 3)
{
elseInd();}
else if (document.Insur ance.State.sele ctedIndex == 4)
{
elseIw();}
else if (document.Insur ance.State.sele ctedIndex == 6)
{
elseMich();}
else if (document.Insur ance.State.sele ctedIndex == 7)
{
elseMin();}
else if (document.Insur ance.State.sele ctedIndex == 8)
{
elseMis();}
else if (document.Insur ance.State.sele ctedIndex == 9)
{
elseNeb();}
else if (document.Insur ance.State.sele ctedIndex == 10)
{
elseND();}
else if (document.Insur ance.State.sele ctedIndex == 11)
{
elseOh();}
else if (document.Insur ance.State.sele ctedIndex == 12)
{
elseSD();}
else if (document.Insur ance.State.sele ctedIndex == 13)
{
elseWI();}
else if (document.Insur ance.State.sele ctedIndex == 5)
{
elseMar();
}
and this doesn't:
switch (document.Insur ance.State.sele ctedIndex)
{
case 1:
ifIll();
break;
case 2:
elseKan();
break;
case 3:
elseInd();
break;
case 4:
elseIw();
break;
case 5:
elseMar();
break;
casce 6:
elseMich();
break;
case 7:
elseMin();
break;
case 8:
elseMis();
break;
case 9:
elseNeb();
break;
case 10:
elseND();
break;
case 11:
elseOh();
break;
case 12:
elseSD();
break;
case 13:
elseWI();
break;
default:
break;
}
I also tried : case "1" etc and case '1' but that too doesnt work. Any
further ideas? Thanks
Try adding:
alert(index); to your default case before the break. As I mentioned
before, if none of the other cases fire, the default will. If it
doesn't, then your problem is not with the contents of the switch
case, but rather the fact that it's not being called at all. If this
is the case, you should backtrack a little, and add alerts at in
various places before the switch case, to make sure that the correct
variable is being passed. My quess is that a null value is being
passed to the switch case, in which case, the default case should pick
it up, and the alert clause should tell you what's what.
If you are still having problems, please Email me a copy of the code
you wish to clean up, and I will be happy to help further.
Navodit said:
>Thanks for all the feedback and comments. First of all I have not written those function names, my job is first to clean up the code as much as possible and then think about renaming. Each of those functions corresponds to around 50-100 lines of code so it is indeed useful to have them as separate functions. Each of those functions populates dropdown with counties for that particular state so when i say it doesnt work that implies the dropdown doesn't get populated when I use the case statement while it does get populated if the if statements are used.
You haven't really made a case for separate functions.
If the functions differ only by what data they use, then
you should be using an array, rather than a switch.
--
Lee said:
> Navodit said:
>>Thanks for all the feedback and comments. First of all I have not written those function names, my job is first to clean up the code as much as possible and then think about renaming. Each of those functions corresponds to around 50-100 lines of code so it is indeed useful to have them as separate functions. Each of those functions populates dropdown with counties for that particular state so when i say it doesnt work that implies the dropdown doesn't get populated when I use the case statement while it does get populated if the if statements are used.
You haven't really made a case for separate functions. If the functions differ only by what data they use, then you should be using an array, rather than a switch.
Or, in this case, maybe a data structure built on Objects.
Here's a simplified example of what I mean:
<html>
<head>
<script type="text/javascript">
var stateData = {
IN: {
name: "Indiana",
capital: "Indianapol is",
counties: [ "Adams","Allen" ,"Bartholomew", "..." ]
},
IL: {
name: "Illinois",
capital: "Springfiel d",
counties: [ "Adams", "Alexander" , "Bond", "..." ]
},
KS: {
name: "Kansas",
capital: "Topeka",
counties: [ "Allen", "Anderson", "Atchison", "..." ]
}
}
function populateState(s el) {
if(sel.selected Index) {
var stateInfo=state Data[sel.options[sel.selectedInd ex].text];
msg="State: "+stateInfo.nam e+"\n"
+"Capital: "+stateInfo.cap ital+"\n"
+"Counties:\ n";
for(var i=0;i<stateInfo .counties.lengt h;i++) {
msg+=" "+stateInfo.cou nties[i]+"\n";
}
alert(msg);
}
}
</script>
</head>
<body>
<form>
<select onchange="popul ateState(this)" >
<option>Choos e a State</option>
<option>IL</option><option> KS</option><option> IN</option>
</select>
</form>
</body>
</html>
-- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Joe Stevenson |
last post by:
Hi all,
I skimmed through the docs for Python, and I did not find anything like
a case or switch statement. I assume there is one and that I just
missed it. Can someone please point me to the appropriate document, or
post an example? I don't relish the idea especially long if-else
statements.
Joe
|
by: Robert Scheer |
last post by:
Hi.
In VBScript I can use a Select Case statement like that:
Select Case X
Case 1 to 10 'X is between 1 and 10
Case 11,14,16 'X is 11 or 14 or 16
End Select
|
by: richasaraf |
last post by:
Hello everyone,
Please HELP !!!!!
I'm facing problem in converting CASE statements into DECODE. As i have PL/SQL 8i, so it does not handle CASE statements. Please send me the solutions . Then basic problem is the CASE has NOT condition i.e. <> ...... and this particularly i'm not able to convert in DECODE.
Please send me the solution as early as possible.. I'm stuck up !!!
I'm giving below the CASE statements and the DECODE...
|
by: clueless_google |
last post by:
hello. i've been beating my head against a wall over this for too
long.
setting the variables 'z' or 'y' to differing numbers, the following
'if/else' code snippet works fine; however, the 'case' code snippet
does not. (the code's function is illustrative.)
//////////////////////////////////////////
//////// working 'if/else' switch ////////
//////////////////////////////////////////
|
by: Chih-Hsu Yen |
last post by:
I encountered a strange problem about switch-case statement.
switch(cmd)
{
case 1: statements; break;
case 2: statements; break;
... ....
case 11: S1;
S2;
S3;
statements;
| |
by: Brett Romero |
last post by:
If I want to use:
switch (AppName)
{
case ApplicationName.App1:
loadApp1Logo();
break;
case ApplicationName.App2:
loadApp2Logo();
break;
|
by: Patrick A |
last post by:
All,
I rely on nested IF statements with multiple conditions heavily, and
someone suggested recently writing the statements (and especially
reading them months later) would be much easier if I used Case
statements instead.
I've read several different examples of Case statements, but can't
quite figure out the syntax, or if they can be used when you need to
test for a pair of conditions, as I am doing below.
|
by: Adem |
last post by:
C/C++ language proposal:
Change the 'case expression' from "integral constant-expression" to "integral expression"
The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
says under 6.4.2(2) :
case constant-expression :
I propose that the case expression of the switch statement
be changed from "integral constant-expression" to "integral expression".
|
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...
|
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,...
|
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...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
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...
|
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
| |