Skip to content

Instantly share code, notes, and snippets.

@clementsjj
Created December 5, 2019 23:12
Show Gist options
  • Save clementsjj/eca3d517089039d6f480cbb173b343e7 to your computer and use it in GitHub Desktop.
Save clementsjj/eca3d517089039d6f480cbb173b343e7 to your computer and use it in GitHub Desktop.
function getOrdinal(addressToChange) {
const streetNames = ['street', 'road', 'avenue'];
let address = addressToChange.toLowerCase().split(' ');
const number = address[address.length - 2];
const isNumber = !isNaN(number);
const isTeenNum = number % 100;
const isNotTeenNum = number % 10;
let ordinal = '';
if (!isNumber) {
return addressToChange;
}
// Check to see if address is a street, road, or avenue
// Which theoretically are the only roads that include a number that needs
// to be changed
const found = streetNames.some(r => address.indexOf(r) >= 0);
// If found = false, then return address, otherwise continue changing number
if (!found) {
return addressToChange;
}
if (isTeenNum >= 10 && isTeenNum <= 20) {
ordinal = 'th';
} else if (isNotTeenNum === 1) {
ordinal = 'st';
} else if (isNotTeenNum === 2) {
ordinal = 'nd';
} else if (isNotTeenNum === 3) {
ordinal = 'rd';
} else {
ordinal = 'th';
}
address[address.length - 2] = number + ordinal;
address = address
.map(s => s.charAt(0).toUpperCase() + s.substring(1))
.join(' ');
return address;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment