17
Description :
Given a string as input, check if it is a valid ordinal number in English or not. If it is valid return truthy value otherwise return falsy value. (Suggested by @Arnauld. Thanks . Also by @JoKing)
For users who want to know about ordinal numbers go here :
https://www.mathsisfun.com/numbers/cardinal-ordinal-chart.html (Suggestion by : qwr)
Possible inputs :
21st ---> true
12nd ---> false
1nd ---> false
....
This is a code golf challenge so shortest code in each language will be the winner.
Examples :
console.log('12th' , true) // This evaluates to true
console.log('1st' , true) // also evaluates to true
console.log('21nd' , false) // returns false
console.log('11st' , false) // returns false
console.log('111199231923819238198231923213123909808th' , true) // true
Since a lot of people asked the question regarding whether input will be only valid strings or not :
All inputs will always be valid. i.e they will be in the form of string and consist of a digit (or number of digits) along with one of the four suffixes :
st
, nd
, rd
, th
Can you clarify the rules of ordinal numbers? Or at least put a link to what the rules you are following. – qwr – 2018-04-14T17:00:54.793
They are normal rules. I changed nothing. But thanks for the input , I added a link – Muhammad Salman – 2018-04-14T17:02:50.977
@Jonathan Allan Ordinal numbers start from
– Oliver Ni – 2018-04-14T23:18:44.5571st
, negative ordinals do not exist - https://english.stackexchange.com/questions/309713/ordinal-form-of-negative-numbers-especially-1-2-3@JonathanAllan OP says "Input is going to be valid ordinal pattern." which means no negatives – Oliver Ni – 2018-04-15T05:12:25.157
I updated the description. – Muhammad Salman – 2018-04-15T05:24:46.677
@JonathanAllan : Yes I mean that every given input will be in the format
1st
so digit along with one of the fourst
,nd
,rd
,th
. – Muhammad Salman – 2018-04-15T12:43:01.610@JonathanAllan : The question has been updated – Muhammad Salman – 2018-04-15T13:30:46.433
I'd recommend changing true/false values to truthy/falsey values and removing the excess part about output being a string or array (unless you actually mean input??)
– Jo King – 2018-04-20T12:54:22.220@JoKing : I don't remember it being here. I will remove that. as for number 1 ok will do that too – Muhammad Salman – 2018-04-20T13:11:49.970
2You say the inputs will always be valid but I think a better term would be well formed. Both 12th and 12nd are well formed but only the former is valid. – David Conrad – 2018-04-20T21:05:34.200
When an ordinal is written we tend to see the suffix and assume it's an ordinal without checking whether that suffix is correct. I often write things like "4rd" just to see if anybody notices. – Esolanging Fruit – 2018-04-22T06:28:16.337