View Full Version : switch with multiple elements in each case...?

10-15-2003, 12:15 AM
I am doing a switch statement that will post an alert for various cases. The prob is I can' t seem to find an example of this anywhere.

There are 50 (states) and 6 regions to hold them...
I want the user's input compared to these regions and tell them where they live. Amazing, this seems so simple, but I'm going nuts.


Should I just do each state in a case?
Put them each in arrays specific to their region and then do cases by index?? (how is this done?)

Oh dear! Thanks in advance...!

10-15-2003, 12:25 AM
Well, you can do it object oriented if you wish, or maybe as a simple array... For regions 0-5 (you could use a string with the region name instead of the number if you wish):

[string nameOfState]:[number regionNumber],
[string nameOfState]:[number regionNumber],
[string nameOfState]:[number regionNumber]

if(typeof oStates[[string userInput]]!='undefined')
alert([string userInput]+' is in region '+oStates[[string userInput]]);If you want to do it the other way, you'll have to search through each region for the state in question, so I think the array is probably better. An example:
[string nameOfRegion]:[string spaceSeparatedListOfStates],
[string nameOfRegion]:[string spaceSeparatedListOfStates]
if((new Regexp('\b'+[string userInput]+'\b')).test(oRegions[l]))
alert([string userInput]+' is in region '+oRegion[l]);

10-15-2003, 12:47 AM
lioreon! Wow! You are skilled at this! THANKS!

Strangely enough, we haven't learned the OO methods of this language yet. I mean, this assignment was a stretch, which is wonderful, but we hadn't covered ways to utilize an array in this way!

So - I did a "stupid" switch with each state in a case and its' corresponding alert, but I could do a variable like

var state={
["VA"] : [southern], ["NY"] : [northern]


and the other default directions here...?

I think I'm needing more specifics, but I will try this when I get off here....I am still a tad slow on the exact punctuations and such!

But! Hey - I'm impressed! Thank you so much again!

10-15-2003, 12:57 AM
liorean ?
this ":" thing - what is it called, please?

your mechanical expertise is what I was seeking....just don't know (hadn't learned/didn't find the source yet) what is used
for this type of a "for... in" type of statement.

thx in advance~

10-15-2003, 08:15 AM
The way I do it, you eliminate the loop...

However, to clarify the syntax:
'VA' : 'Southern',
'NY' : 'Northern'
That is called an object literal, and is equivalent to doing this:
oStates=new Object;
In other words, you create an object with the states as properties, and the regions as property values. Then you could take the next step and make the states and regions objects instead of strings, but I don't see why you'd do that in your case.

10-15-2003, 05:56 PM
Hi again.
After the last note, I discovered this looked close to "indexing" though it is more pertinent to my needs. That was why I asked about the colon. Didn't know where it was from when I first wrote.

The setup you have WITHOUT the loop esp is nice since I still get tangled up in them :0 Thing is, I have to test the user's input and have multiple elements (all the states) .

Would I just expand the items in the brackets? such as

var oRegions=new Object;
oRegions['VA', 'GA'] : 'southern';


Will do some research on Object Literals.
I am not clear on the complete flow until I see more...and I will try this asap to see what I come up with!

- many thanks!

10-15-2003, 06:40 PM
Well, you'll have to add one name of state:name of region for each state you want to add. That means you'll have to add fifty entries that way.

10-15-2003, 06:49 PM
...and about testing each condition, just use that 'while...if'
loop you so adeptly wrote on first response....
jeepers! you're good at this!

From the information in our texts and class, there was no way I could come up with this configuration for a script....!
Can't thank you enough for this new challenge in code construction! You've helped tremendously and left me.......
in a better state! (sorry - couldn't resist.)

~mtm :o