Skip to content

Commit 044c4bf

Browse files
committed
refactor function to improve ordinal suffix handling for 0 [4-9] [11-13]
1 parent 2c0fb31 commit 044c4bf

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

‎practice-tdd/get-ordinal-number.js‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
export function getOrdinalNumber(num) {
22
const numStr = num.toString();
33
const numLength = numStr.length;
4+
const pattern1 = /^(11|12|13)$/;
5+
const pattern2 = /^0$/;
6+
const pattern3 = /^(4|5|6|7|8|9)$/;
7+
const last2digits = numStr.slice(-2);
8+
const last1digit = numStr.slice(-1);
9+
410
if (numStr[numLength - 1] == "1" && numStr[numLength - 2] != "1") {
511
return `${numStr}st`;
612
} else if (numStr[numLength - 1] == "2" && numStr[numLength - 2] != "1") {
713
return `${numStr}nd`;
814
} else if (numStr[numLength - 1] == "3" && numStr[numLength - 2] != "1") {
915
return `${numStr}rd`;
16+
} else if (
17+
pattern1.test(last2digits) ||
18+
pattern2.test(last1digit) ||
19+
pattern3.test(last1digit)
20+
) {
21+
return `${numStr}th`;
1022
}
11-
12-
//return "1st";
1323
}

0 commit comments

Comments
 (0)