Skip to content

Commit 1570edd

Browse files
committed
Implement getOrdinalNumber function and add comprehensive test cases
1 parent f84c03e commit 1570edd

File tree

2 files changed

+77
-4
lines changed

2 files changed

+77
-4
lines changed
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
function getOrdinalNumber(num) {
2-
return "1st";
1+
function getOrdinalNumber(input) {
2+
const lastDigit = input % 10;
3+
const lastTwoDigits = input % 100;
4+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
5+
return input + "th";
6+
} else if (lastDigit === 1) {
7+
return input + "st";
8+
} else if (lastDigit === 2) {
9+
return input + "nd";
10+
} else if (lastDigit === 3) {
11+
return input + "rd";
12+
} else {
13+
return input + "th";
14+
}
315
}
4-
516
module.exports = getOrdinalNumber;

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

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,69 @@ const getOrdinalNumber = require("./get-ordinal-number");
77
// Case 1: Identify the ordinal number for 1
88
// When the number is 1,
99
// Then the function should return "1st"
10-
1110
test("should return '1st' for 1", () => {
1211
expect(getOrdinalNumber(1)).toEqual("1st");
1312
});
13+
14+
// Case 2: Identify the ordinal number for 2
15+
// When the number is 2,
16+
// Then the function should return "2nd"
17+
test('should return "2nd" for input 2', () => {
18+
expect(getOrdinalNumber(2)).toBe("2nd");
19+
});
20+
21+
// Case 3: Identify the ordinal number for 3
22+
// When the number is 3,
23+
// Then the function should return "3rd"
24+
test('should return "3rd" for input 3', () => {
25+
expect(getOrdinalNumber(3)).toBe("3rd");
26+
});
27+
28+
// Case 4: Handle special case for 11
29+
// When the number is 11,
30+
// Then the function should return "11th"
31+
test('should return "11th" for input 11', () => {
32+
expect(getOrdinalNumber(11)).toBe("11th");
33+
});
34+
35+
// Case 5: Handle special case for 12
36+
// When the number is 12,
37+
// Then the function should return "12th"
38+
test('should return "12th" for input 12', () => {
39+
expect(getOrdinalNumber(12)).toBe("12th");
40+
});
41+
42+
// Case 6: Handle special case for 13
43+
// When the number is 13,
44+
// Then the function should return "13th"
45+
test('should return "13th" for input 13', () => {
46+
expect(getOrdinalNumber(13)).toBe("13th");
47+
});
48+
49+
// Case 7: Identify the ordinal number for 21
50+
// When the number is 21,
51+
// Then the function should return "21st"
52+
test('should return "21st" for input 21', () => {
53+
expect(getOrdinalNumber(21)).toBe("21st");
54+
});
55+
56+
// Case 8: Identify the ordinal number for 102
57+
// When the number is 102,
58+
// Then the function should return "102nd"
59+
test('should return "102nd" for input 102', () => {
60+
expect(getOrdinalNumber(102)).toBe("102nd");
61+
});
62+
63+
// Case 9: Identify the ordinal number for 113
64+
// When the number is 113,
65+
// Then the function should return "113th"
66+
test('should return "113th" for input 113', () => {
67+
expect(getOrdinalNumber(113)).toBe("113th");
68+
});
69+
70+
// Case 10: Identify the ordinal number for 100
71+
// When the number is 100,
72+
// Then the function should return "100th"
73+
test('should return "100th" for input 100', () => {
74+
expect(getOrdinalNumber(100)).toBe("100th");
75+
});

0 commit comments

Comments
 (0)