Challenge
Write a function that will return an inputted numerical year in century format. The output should have the appropriate written ending ('st','nd','rd','th') as well.
Examples
In: 2259
Out: 23rd
In: 1124
Out: 12th
In: 2000
Out: 21st
Good luck!
This challenge comes from Cpt.ManlyPink on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Oldest comments (19)
This would've been even more interesting using the strict usage of "century" ;-)
First handle the exceptions (i.e. 11 - 13), then just use the last digit to decide.
F#
Yes, yes, I know, 2000 should return "21st" century. I don't know of anyone who counts centuries like that, so my function returns them according to normal use.
A reasonably short and reasonably Rusty solution:
Garna robota!
Good job!
Diakuyu :)
Thanks :)
I used your answer as a cheatsheet of sorts. Good job! :)
Oh, good catch
A bit of functional JS
I like your approach, it looks very clean.
This is probably too fringe to matter in most contexts, but wouldn't your function return 111st for the year 11,092?
It would, indeed. Thanks for pointing out. Now I have fixed it and added a new test case.
OLD SOLUTION (Line 7)
UPDATED SOLUTION (Line 7)
Rust:
It's fixed now (I think). It should also support BC centuries.
Thanks