Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    mtm
    mtm is offline
    Registered User
    Join Date
    Oct 2003
    Location
    US
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question switch with multiple elements in each case...?

    not-a-pro-yet...
    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.

    Suggestions?

    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...!
    mtm

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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):
    Code:
    var
        oStates={
            [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:
    Code:
    var
        oRegions={
            [string nameOfRegion]:[string spaceSeparatedListOfStates],
            ...,
            [string nameOfRegion]:[string spaceSeparatedListOfStates]
        },
        l=oRegions.length;
    while(l-->0)
        if((new Regexp('\b'+[string userInput]+'\b')).test(oRegions[l]))
            alert([string userInput]+' is in region '+oRegion[l]);
    Last edited by liorean; 10-15-2003 at 12:33 AM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    mtm
    mtm is offline
    Registered User
    Join Date
    Oct 2003
    Location
    US
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!
    sorry!

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

  • #4
    mtm
    mtm is offline
    Registered User
    Join Date
    Oct 2003
    Location
    US
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    also....

    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~
    mtm

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    The way I do it, you eliminate the loop...

    However, to clarify the syntax:
    Code:
    var
        oStates={
             'VA' : 'Southern',
             'NY' : 'Northern'
        }
    That is called an object literal, and is equivalent to doing this:
    Code:
    var
        oStates=new Object;
    oStates['VA']='Southern';
    oStates['NY']='Northern';
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    mtm
    mtm is offline
    Registered User
    Join Date
    Oct 2003
    Location
    US
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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';

    etc?


    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!
    mtm

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    mtm
    mtm is offline
    Registered User
    Join Date
    Oct 2003
    Location
    US
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    hope I'm not being a bother....

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •