Skip to content

Commit afc85d7

Browse files
committed
Added 5-th case and solution for them to get-ordinal-number
1 parent b516a61 commit afc85d7

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

Sprint-3/2-practice-tdd/get-ordinal-number.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
function getOrdinalNumber(num) {
2-
if (num === 1) {
3-
return "1st";
4-
}
5-
if (num === 2) {
6-
return "2nd";
7-
}
8-
if (num === 3) {
9-
return "3rd";
2+
const lastDigit = num % 10;
3+
const lastTwoDigits = num % 100;
4+
5+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
6+
return num + "th";
107
}
8+
9+
if (lastDigit === 1) return num + "st";
10+
if (lastDigit === 2) return num + "nd";
11+
if (lastDigit === 3) return num + "rd";
12+
1113
return num + "th";
1214
}
1315

Sprint-3/2-practice-tdd/get-ordinal-number.test.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,20 @@ test("should return '3rd' for 3", () => {
2929
});
3030

3131
// Case 4: Identify the ordinal number for other numbers
32-
// When the number is 11,
33-
// Then the function should return "11th"
34-
35-
test("should return '11th' for 11", () => {
36-
expect(getOrdinalNumber(11)).toEqual("11th");
32+
// If the last digit of number is not 1, 2, 3
33+
// and the last two digits are not between 11 and 13
34+
// Then the function should return the number with "th" suffix
35+
36+
test("should return '44th' for 44", () => {
37+
expect(getOrdinalNumber(44)).toEqual("44th");
38+
expect(getOrdinalNumber(9)).toEqual("9th");
39+
expect(getOrdinalNumber(20)).toEqual("20th");
40+
expect(getOrdinalNumber(100)).toEqual("100th");
41+
expect(getOrdinalNumber(111)).toEqual("111th");
42+
expect(getOrdinalNumber(112)).toEqual("112th");
43+
expect(getOrdinalNumber(113)).toEqual("113th");
44+
expect(getOrdinalNumber(131)).toEqual("131st");
45+
expect(getOrdinalNumber(222)).toEqual("222nd");
46+
expect(getOrdinalNumber(323)).toEqual("323rd");
47+
expect(getOrdinalNumber(4046486655)).toEqual("4046486655th");
3748
});

0 commit comments

Comments
 (0)