Skip to content

Instantly share code, notes, and snippets.

@MVAodhan
Created September 26, 2022 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MVAodhan/d2cd75c87e14e00084cd4b7ed81ef440 to your computer and use it in GitHub Desktop.
Save MVAodhan/d2cd75c87e14e00084cd4b7ed81ef440 to your computer and use it in GitHub Desktop.
/**
* Write a function to output the ordinal suffix of a positive integer
* concatenated to an inputted number.
*
* > ordinal(3)
> '3rd'
> ordinal(57)
> '57th'
*/
const ordinal = (num) => {
if (!num) return 'cannot give an ordinal to nothing';
num = num.toString();
let numArray = [...num];
let lastDigit = numArray[numArray.length - 1];
switch (true) {
case num == '11' ||
num == '12' ||
num == '13' ||
lastDigit == '4' ||
lastDigit == '5' ||
lastDigit == '6' ||
lastDigit == '7' ||
lastDigit == '8' ||
lastDigit == '9' ||
lastDigit == '0':
return `${num}th`;
break;
case lastDigit == '1':
return `${num}st`;
break;
case lastDigit == '2':
return `${num}nd`;
break;
case lastDigit == '3':
return `${num}rd`;
break;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment