diff --git a/README.md b/README.md index b23877de1..ee07a577e 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ * [Median of array of numbers](#median-of-array-of-numbers) * [Object from key value pairs](#object-from-key-value-pairs) * [Object to key value pairs](#object-to-key-value-pairs) +* [Ordinal suffix of number](#ordinal-suffix-of-number) * [Percentile](#percentile) * [Pick](#pick) * [Pipe](#pipe) @@ -486,6 +487,22 @@ const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]); // objectToPairs({a: 1, b: 2}) -> [['a',1],['b',2]]) ``` +### Ordinal suffix of number + +Use the modulo operator (`%`) to find values of single and tens digits. +Find which ordinal pattern digits match. +If digit is found in teens pattern, use teens ordinal. + +```js +const toOrdinalSuffix = num => { + const int = parseInt(num), digits = [(int % 10), (int % 100)], + ordinals = ["st", "nd", "rd", "th"], oPattern = [1,2,3,4], + tPattern = [11, 12, 13, 14, 15, 16, 17, 18, 19] + return oPattern.includes(digits[0]) && !tPattern.includes(digits[1]) ? int + ordinals[digits[0]-1] : int + ordinals[3]; +} +// toOrdinalSuffix("123") -> "123rd" +``` + ### Percentile Use `Array.reduce()` to calculate how many numbers are below the value and how many are the same value and diff --git a/snippets/get-ordinal-suffix-of-number.md b/snippets/ordinal-suffix-of-number.md similarity index 94% rename from snippets/get-ordinal-suffix-of-number.md rename to snippets/ordinal-suffix-of-number.md index f8832638b..4e849eff7 100644 --- a/snippets/get-ordinal-suffix-of-number.md +++ b/snippets/ordinal-suffix-of-number.md @@ -1,4 +1,4 @@ -### Get Ordinal Suffix of Number +### Ordinal suffix of number Use the modulo operator (`%`) to find values of single and tens digits. Find which ordinal pattern digits match.