Last active
August 5, 2023 10:37
-
-
Save thathurtabit/160f4197ea87c987c0caa6fd6d64e39b to your computer and use it in GitHub Desktop.
Get Number Suffix (-st, -nd, -rd, -th)
This file contains hidden or 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
export const getNUmberSuffix = (number: number): string => { | |
const thExceptions = (number: number) => | |
[11, 12, 13].some((exception) => exception === number); | |
const lastDigit = number % 10; | |
if (thExceptions(number) || lastDigit === 0 || lastDigit > 3) | |
return `${number}th`; | |
if (lastDigit === 1) return `${number}st`; | |
if (lastDigit === 2) return `${number}nd`; | |
return `${number}rd`; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Log out to view:
const logOutNumber = new Array(31).fill(0).forEach((_,index) => console.log(getNumberSuffix(index + 1)))