Skip to content

Commit 50eaeb4

Browse files
committed
Implement getOrdinalNumber to handle ordinal suffixes correctly
- Return proper suffixes for 1st, 2nd, 3rd, and others (e.g., 4th, 11th, 23rd) - Added Jest tests for various edge cases including teens (11th-13th) - Improved function to handle general inputs dynamically
1 parent 6d8d7bb commit 50eaeb4

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const suffixes = ["th", "st", "nd", "rd"];
3+
const value = num % 100;
4+
if (value >= 11 && value <= 13) {
5+
return num + "th";
6+
}
7+
const lastDigit = num % 10;
8+
const suffix = suffixes[lastDigit] || "th";
9+
return num + suffix;
310
}
411

5-
module.exports = getOrdinalNumber;
12+
module.exports = getOrdinalNumber;

Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,20 @@ const getOrdinalNumber = require("./get-ordinal-number");
1111
test("should return '1st' for 1", () => {
1212
expect(getOrdinalNumber(1)).toEqual("1st");
1313
});
14+
15+
test("should return '2nd' for 2", () => {
16+
expect(getOrdinalNumber(2)).toEqual("2nd");
17+
});
18+
19+
test("should return '3rd' for 3", () => {
20+
expect(getOrdinalNumber(3)).toEqual("3rd");
21+
});
22+
23+
test("should return '4th' for 4", () => {
24+
expect(getOrdinalNumber(4)).toEqual("4th");
25+
});
26+
27+
test("should return '11th' for 11", () => {
28+
expect(getOrdinalNumber(11)).toEqual("11th");
29+
});
30+

0 commit comments

Comments
 (0)