SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple "OR" in JS

    Hi all,

    I have a Javascript project on where I need to sift through a bunch of 4 digit Aussie post code ranges, and assign a State/Territory depending on the range. For example given the following ranges:

    3000 - 3138, 3140 - 3207, 3428, 3752, 3765, 3793, 3796, 3802 - 3806, 3910, 3930/1, 3975 - 3977 = MELBOURNE
    4000 - 4179, 4205 - 4207, 4300 - 4305, 4500 - 4503 = BRISBANE
    etc etc....

    If a user put in one of these, they should be assigned the code "MELBOURNE", or "BRISBANE" respectively.

    Normally I would run the input through a switch, runing a match for each individual range case by case - but with so many postcodes to sift through per state/Territory, the switch would be a mile long in depth!

    So Im thinking it would be a good idea to test every range for a given state in a single case (I hope that makes sense!). My question is, is this a good/safe way to do things?

    Example:

    case (((intPostCode >= 3000) && (intPostCode <= 3138))
    ||((intPostCode >= 3140) && (intPostCode <= 3207))
    ||(intPostCode == 3428)
    ||(intPostCode == 3752)
    ||(intPostCode == 3765)
    ||(intPostCode == 3793)
    ||(intPostCode == 3796)
    ||((intPostCode >= 3802) && (intPostCode <= 3806))
    ||(intPostCode == 3910)
    ||(intPostCode == 3930)
    ||((intPostCode >= 3975) && (intPostCode <= 3977))
    ):
    myState = "MELBOURNE";
    break;

    I know this a bit cumbersome, but with no access to a database, and a low budget, I cannot think of a better idea.

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    // Defining all the mail code ranges in an object is only a little less code than your method,
    // and your method is more efficient than multiple object lookups and compares through a loop.
    //That said, this code returns the US state from its zip code- use it if you like, with your values.

    Code:
    function stateFromZip(zip){
    	zip= Number(zip);
    	var A, statezips={
    		AK:[99501, 99950], AL:[35004, 36925], AR:[71601, 72959], AR1:[75502, 75502],
    		AZ:[85001, 86556], CA:[90001, 96162], CO:[80001, 81658], CT:[6001, 6389],
    		CT2:[6401, 6928], DC:[20001, 20039], DC2:[20042, 20599], DC3:[20799, 20799],
    		DE:[19701, 19980], FL:[32004, 34997], GA:[30001, 31999], GA2:[39901, 39901],
    		HI:[96701, 96898], IA:[50001, 52809], IA2:[68119, 68120], ID:[83201, 83876],
    		IL:[60001, 62999], IN:[46001, 47997], KS:[66002, 67954], KY:[40003, 42788],
    		LA:[70001, 71232], LA2:[71234, 71497], MA:[1001, 2791], MA2:[5501, 5544],
    		MD:[20331, 20331], MD2:[20335, 20797], MD3:[20812, 21930], ME:[3901, 4992],
    		MI:[48001, 49971], MN:[55001, 56763], MO:[63001, 65899], MS:[38601, 39776],
    		MS2:[71233, 71233], MT:[59001, 59937], NC:[27006, 28909], ND:[58001, 58856],
    		NE:[68001, 68118], NE2:[68122, 69367], NH:[3031, 3897], NJ:[7001, 8989],
    		NM:[87001, 88441], NV:[88901, 89883], NY:[6390, 6390], NY2:[10001, 14975],
    		OH:[43001, 45999], OK:[73001, 73199], OK2:[73401, 74966], OR:[97001, 97920],
    		PA:[15001, 19640], RI:[2801, 2940], SC:[29001, 29948], SD:[57001, 57799],
    		TN:[37010, 38589], TX:[73301, 73301], TX2:[75001, 75501], TX3:[75503, 79999],
    		TX4:[88510, 88589], UT:[84001, 84784], VA:[20040, 20041], VA2:[20040, 20167],
    		VA3:[20042, 20042], VA4:[22001, 24658], VT:[5001, 5495], VT2:[5601, 5907],
    		WA:[98001, 99403], WI:[53001, 54990], WV:[24701, 26886], WY:[82001, 83128]
    	}
    	for(var z in statezips){
    		A= statezips[z];
    		if(statezips.hasOwnProperty(z) && zip> A[0] && zip<A[1]){
    			return  z.substring(0, 2);
    		}
    	}
    	return zip;
    }
    stateFromZip('04355');

    /* returned value: (String)
    ME
    */

  3. #3
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for such a comprehensive reply! I really appreciate the help.


Bookmarks

Posting Permissions

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