Created
September 26, 2022 23:19
-
-
Save MVAodhan/d2cd75c87e14e00084cd4b7ed81ef440 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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