Created
March 22, 2021 10:11
-
-
Save HashirHussain/c352de3271a7805354f53055811ca386 to your computer and use it in GitHub Desktop.
Convert number to ordinal format
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
function numberToOrdinal(input) { | |
try { | |
if(typeof input !== 'number') { | |
throw('Please provide valid number'); | |
} | |
const lastDigit = input % 10; | |
let ordinal = ''; | |
if(lastDigit === 1 && input !== 11) { | |
ordinal = 'st'; | |
} else if (lastDigit === 2 && input !== 12) { | |
ordinal = 'nd'; | |
} else if (lastDigit === 3 && input !== 13) { | |
ordinal = 'rd'; | |
} else { | |
ordinal = 'th'; | |
} | |
return input + ordinal; | |
} catch(e) { | |
console.log(e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment