Computing State from Postcode (Zip Code)

From Q
Jump to navigation Jump to search

In Australia, all addresses have a postcode (i.e., a zip code), which is a three- or four digit number where the first digit identifies the state or territory of residence. If stored as a Text Variable, it could be converted to a categorical variable with the following code:

var str = postcodevariable;
var res = parseInt(str);

if ((res >= 200 && res <= 299) || (res >= 2600 && res <= 2619) || (res >= 2900 && res <=2920)) 1; //ACT
else if ((res >= 1000 && res <= 2599) || (res >= 2620 && res <= 2899) || (res >= 2921 && res <=2999)) 2; //NSW
else if ((res >= 3000 && res <= 3999) || (res >= 8000 && res <= 8099)) 3; //Victoria
else if ((res >= 4000 && res <= 4999) || (res >= 9000 && res <= 9099)) 4; //Queensland
else if (res >= 5000 && res <= 5999) 5; //SA
else if (res >= 6000 && res <= 6999) 6; //WA
else if (res >= 7000 && res <= 7999) 7; //TAS
else if (res >= 800 && res <= 999) 8; //NT
else 99;

The resulting question should be set to a Question Type of Pick One.