Skip to content

Commit 8ec64a7

Browse files
committed
implemented and wrote test cases for getOrdinalNumber function
1 parent b303cc2 commit 8ec64a7

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastTwoDigits = num % 100;
3+
const lastDigit = num % 10;
4+
5+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
6+
return `${num}${"th"}`;
7+
}
8+
9+
switch (lastDigit) {
10+
case 1:
11+
return `${num}${"st"}`;
12+
case 2:
13+
return `${num}${"nd"}`;
14+
case 3:
15+
return `${num}${"rd"}`;
16+
default:
17+
return `${num}${"th"}`;
18+
}
319
}
420

21+
console.log(getOrdinalNumber(3));
22+
523
module.exports = getOrdinalNumber;

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,29 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
1818
expect(getOrdinalNumber(21)).toEqual("21st");
1919
expect(getOrdinalNumber(131)).toEqual("131st");
2020
});
21+
22+
// Case 2: Numbers ending in 2 → add nd → (2nd, 22nd, 42nd)
23+
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
24+
expect(getOrdinalNumber(2)).toEqual("2nd");
25+
expect(getOrdinalNumber(22)).toEqual("22nd");
26+
expect(getOrdinalNumber(142)).toEqual("142nd");
27+
});
28+
// Case 3: Numbers ending in 3 → add rd → (3rd, 23rd, 53rd)
29+
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
30+
expect(getOrdinalNumber(3)).toEqual("3rd");
31+
expect(getOrdinalNumber(33)).toEqual("33rd");
32+
expect(getOrdinalNumber(153)).toEqual("153rd");
33+
});
34+
// Case 4: All other numbers → add th → (4th, 6th, 20th, 100th)
35+
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
36+
expect(getOrdinalNumber(4)).toEqual("4th");
37+
expect(getOrdinalNumber(20)).toEqual("20th");
38+
expect(getOrdinalNumber(100)).toEqual("100th");
39+
});
40+
// Exceptions: Numbers ending in 11, 12, and 13 use -th (e.g., 11th, 12th, 13th).
41+
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
42+
expect(getOrdinalNumber(11)).toEqual("11th");
43+
expect(getOrdinalNumber(12)).toEqual("12th");
44+
expect(getOrdinalNumber(113)).toEqual("113th");
45+
});
46+

0 commit comments

Comments
 (0)