From 54f163b9f105a82a6b225a274cc7a96ceb7b4553 Mon Sep 17 00:00:00 2001 From: Michael Goldspinner Date: Wed, 13 Dec 2017 09:01:14 -0500 Subject: [PATCH 1/3] Get Ordinal Suffix of Number JS Code Snippet to get the ordinal suffix of a given number (int or string). Returns the provided value wtih concatenated ordinal. --- snippets/get-ordinal-suffix-of-number.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 snippets/get-ordinal-suffix-of-number.md diff --git a/snippets/get-ordinal-suffix-of-number.md b/snippets/get-ordinal-suffix-of-number.md new file mode 100644 index 000000000..cda41b9ac --- /dev/null +++ b/snippets/get-ordinal-suffix-of-number.md @@ -0,0 +1,18 @@ +### Get 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 = int => { + int = parseInt(int); + var digits = [ (int % 10), (int % 100)]; + var ordinals = ["st", "nd", "rd", "th"]; + var oPattern = [1,2,3,4]; + var tPattern = [11, 12, 13, 14, 15, 16, 17, 18, 19] + + return pattern.includes(digits[0]) && !teens.includes(digits[1]) ? int + suffix[digits[0]-1] : int + suffix[3]; +} +// toOrdinalSuffix("123") -> "123rd" +``` \ No newline at end of file From 38596dcaa53e54ff087c802b847c62b54f671b5b Mon Sep 17 00:00:00 2001 From: Michael Goldspinner Date: Wed, 13 Dec 2017 23:53:34 -0500 Subject: [PATCH 2/3] Ordinal Suffix hotfix mismatched variables. --- snippets/get-ordinal-suffix-of-number.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/get-ordinal-suffix-of-number.md b/snippets/get-ordinal-suffix-of-number.md index cda41b9ac..725ff05b4 100644 --- a/snippets/get-ordinal-suffix-of-number.md +++ b/snippets/get-ordinal-suffix-of-number.md @@ -12,7 +12,7 @@ const toOrdinalSuffix = int => { var oPattern = [1,2,3,4]; var tPattern = [11, 12, 13, 14, 15, 16, 17, 18, 19] - return pattern.includes(digits[0]) && !teens.includes(digits[1]) ? int + suffix[digits[0]-1] : int + suffix[3]; + return oPattern.includes(digits[0]) && !tPattern.includes(digits[1]) ? int + ordinals[digits[0]-1] : int + ordinals[3]; } // toOrdinalSuffix("123") -> "123rd" ``` \ No newline at end of file From 960fa239bcb1d91976643d899abcd4b424866b7d Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 10:15:43 +0200 Subject: [PATCH 3/3] Update get-ordinal-suffix-of-number.md --- snippets/get-ordinal-suffix-of-number.md | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/snippets/get-ordinal-suffix-of-number.md b/snippets/get-ordinal-suffix-of-number.md index 725ff05b4..f8832638b 100644 --- a/snippets/get-ordinal-suffix-of-number.md +++ b/snippets/get-ordinal-suffix-of-number.md @@ -5,14 +5,11 @@ Find which ordinal pattern digits match. If digit is found in teens pattern, use teens ordinal. ```js -const toOrdinalSuffix = int => { - int = parseInt(int); - var digits = [ (int % 10), (int % 100)]; - var ordinals = ["st", "nd", "rd", "th"]; - var oPattern = [1,2,3,4]; - var 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]; +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" -``` \ No newline at end of file +```