Skip to content

Instantly share code, notes, and snippets.

@Mellen
Last active October 1, 2021 14:12
Show Gist options
  • Save Mellen/d7238882db4a32b359c88cedc892228d to your computer and use it in GitHub Desktop.
Save Mellen/d7238882db4a32b359c88cedc892228d to your computer and use it in GitHub Desktop.
For any non-negative integer, this function supplies the correct English ordinal suffix.
function ordinalSuffix(n)
{
let units = n % 10;
let tens = n % 100;
let ord = (units > 3 || units < 1) || (tens > 10 && tens < 20) ? 'th' : (units == 1 ? 'st' : (units == 2 ? 'nd' : 'rd'));
return ord;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment