Skip to content

Instantly share code, notes, and snippets.

@Memphis1983
Created December 19, 2021 17:19
Show Gist options
  • Save Memphis1983/45352f096ffa7e498f875339beaff172 to your computer and use it in GitHub Desktop.
Save Memphis1983/45352f096ffa7e498f875339beaff172 to your computer and use it in GitHub Desktop.
getFullStateName
/**
* Converts an array of state abbreviations to their full name
* @param {*} arr of strings
*/
function getFullStateName(arr) {
if (!Array.isArray(arr)) return arr;
const states = [];
for (let i = 0; i < arr.length; i++) {
const stateCode = String(arr[i]);
switch (stateCode) {
case "AL":
states.push("Alabama");
break;
case "AK":
states.push("Alaska");
break;
case "AS":
states.push("American Samoa");
break;
case "AZ":
states.push("Arizona");
break;
case "AR":
states.push("Arkansas");
break;
case "CA":
states.push("California");
break;
case "CO":
states.push("Colorado");
break;
case "CT":
states.push("Connecticut");
break;
case "DE":
states.push("Delaware");
break;
case "DC":
states.push("District Of Columbia");
break;
case "FM":
states.push("Federated States Of Micronesia");
break;
case "FL":
states.push("Florida");
break;
case "GA":
states.push("Georgia");
break;
case "GU":
states.push("Guam");
break;
case "HI":
states.push("Hawaii");
break;
case "ID":
states.push("Idaho");
break;
case "IL":
states.push("Illinois");
break;
case "IN":
states.push("Indiana");
break;
case "IA":
states.push("Iowa");
break;
case "KS":
states.push("Kansas");
break;
case "KY":
states.push("Kentucky");
break;
case "LA":
states.push("Louisiana");
break;
case "ME":
states.push("Maine");
break;
case "MH":
states.push("Marshall Islands");
break;
case "MD":
states.push("Maryland");
break;
case "MA":
states.push("Massachusetts");
break;
case "MI":
states.push("Michigan");
break;
case "MN":
states.push("Minnesota");
break;
case "MS":
states.push("Mississippi");
break;
case "MO":
states.push("Missouri");
break;
case "MT":
states.push("Montana");
break;
case "NE":
states.push("Nebraska");
break;
case "NV":
states.push("Nevada");
break;
case "NH":
states.push("New Hampshire");
break;
case "NJ":
states.push("New Jersey");
break;
case "NM":
states.push("New Mexico");
break;
case "NY":
states.push("New York");
break;
case "NC":
states.push("North Carolina");
break;
case "ND":
states.push("North Dakota");
break;
case "MP":
states.push("Northern Mariana Islands");
break;
case "OH":
states.push("Ohio");
break;
case "OK":
states.push("Oklahoma");
break;
case "OR":
states.push("Oregon");
break;
case "PW":
states.push("Palau");
break;
case "PA":
states.push("Pennsylvania");
break;
case "PR":
states.push("Puerto Rico");
break;
case "RI":
states.push("Rhode Island");
break;
case "SC":
states.push("South Carolina");
break;
case "SD":
states.push("South Dakota");
break;
case "TN":
states.push("Tennessee");
break;
case "TX":
states.push("Texas");
break;
case "UT":
states.push("Utah");
break;
case "VT":
states.push("Vermont");
break;
case "VI":
states.push("Virgin Islands");
break;
case "VA":
states.push("Virginia");
break;
case "WA":
states.push("Washington");
break;
case "WV":
states.push("West Virginia");
break;
case "WI":
states.push("Wisconsin");
break;
case "WY":
states.push("Wyoming");
break;
default:
return;
}
}
return states;
}
getFullStateName(["AL", "MS", "GA", "FL", "SC", "NC", "KY", "TN", "VA"]);
/**
* Expected
[ 'Alabama', 
'Mississippi', 
'Georgia', 
'Florida', 
'South Carolina', 
'North Carolina', 
'Kentucky', 
'Tennessee', 
'Virginia' ]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment