Skip to content

Commit 1e36bac

Browse files
committed
simplify getOrdinalNumber function and enhance test cases
1 parent 4c218e9 commit 1e36bac

File tree

2 files changed

+40
-63
lines changed

2 files changed

+40
-63
lines changed
Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
function getOrdinalNumber(num) {
2-
if (num === 1) return "1st";
3-
if (num === 2) return "2nd";
4-
if (num === 3) return "3rd";
5-
if (num === 11) return "11th";
6-
if (num === 21) return "21st";
7-
if (num === 22) return "22nd";
8-
if (num === 23) return "23rd";
9-
return num + "th";
2+
const n = Math.abs(parseInt(num, 10));
3+
const lastTwoDigits = n % 100;
4+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
5+
return `${n}th`;
6+
}
7+
const lastDigit = n % 10;
8+
switch (lastDigit) {
9+
case 1:
10+
return `${n}st`;
11+
case 2:
12+
return `${n}nd`;
13+
case 3:
14+
return `${n}rd`;
15+
default:
16+
return `${n}th`;
17+
}
1018
}
1119

1220
module.exports = getOrdinalNumber;

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

Lines changed: 24 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,35 @@ const getOrdinalNumber = require("./get-ordinal-number");
44
// continue testing and implementing getOrdinalNumber for additional cases
55
// Write your tests using Jest - remember to run your tests often for continual feedback
66

7-
// Case 1: Identify the ordinal number for 1
8-
// When the number is 1,
9-
// Then the function should return "1st"
10-
11-
test("should return '1st' for 1", () => {
12-
expect(getOrdinalNumber(1)).toEqual("1st");
13-
});
14-
15-
// Case 2: Identify the ordinal number for 2
16-
// When the number is 2,
17-
// Then the function should return "2nd"
18-
19-
test("should return '2nd' for 2", () => {
20-
expect(getOrdinalNumber(2)).toEqual("2nd");
7+
// Case 1: Numbers ending in 1 except 11 -> "st"
8+
test("should return correct suffix for numbers ending in 1 except 11", () => {
9+
const stNumbers = [1, 21, 101, 121];
10+
for (const num of stNumbers) {
11+
expect(getOrdinalNumber(num)).toEqual(`${num}st`);
12+
}
2113
});
2214

23-
// Case 3: Identify the ordinal number for 3
24-
// When the number is 3,
25-
// Then the function should return "3rd"
26-
27-
test("should return '3rd' for 3", () => {
28-
expect(getOrdinalNumber(3)).toEqual("3rd");
15+
// Case 2: Numbers ending in 2 except 12 -> "nd"
16+
test("should return correct suffix for numbers ending in 2 except 12", () => {
17+
const ndNumbers = [2, 22, 102, 122];
18+
for (const num of ndNumbers) {
19+
expect(getOrdinalNumber(num)).toEqual(`${num}nd`);
20+
}
2921
});
3022

31-
// Case 4: Identify the ordinal number for 4
32-
// When the number is 4,
33-
// Then the function should return "4th"
34-
35-
test("should return '4th' for 4", () => {
36-
expect(getOrdinalNumber(4)).toEqual("4th");
23+
// Case 3: Numbers ending in 3 except 13 -> "rd"
24+
test("should return correct suffix for numbers ending in 3 except 13", () => {
25+
const rdNumbers = [3, 23, 103, 123];
26+
for (const num of rdNumbers) {
27+
expect(getOrdinalNumber(num)).toEqual(`${num}rd`);
28+
}
3729
});
3830

39-
// Case 5: Identify the ordinal number for 11
40-
// When the number is 11,
41-
// Then the function should return "11th"
42-
43-
test("should return '11th' for 11", () => {
44-
expect(getOrdinalNumber(11)).toEqual("11th");
31+
// Case 4: Numbers ending in 4-9, 0, and 11-13 -> "th"
32+
test("should return correct suffix for numbers ending in 4-9, 0, and 11-13", () => {
33+
const thNumbers = [4, 5, 6, 7, 8, 9, 11, 12, 13, 100];
34+
for (const num of thNumbers) {
35+
expect(getOrdinalNumber(num)).toEqual(`${num}th`);
36+
}
4537
});
4638

47-
// Case 6: Identify the ordinal number for 21
48-
// When the number is 21,
49-
// Then the function should return "21st"
50-
51-
test("should return '21st' for 21", () => {
52-
expect(getOrdinalNumber(21)).toEqual("21st");
53-
});
54-
55-
// Case 7: Identify the ordinal number for 22
56-
// When the number is 22,
57-
// Then the function should return "22nd"
58-
59-
test("should return '22nd' for 22", () => {
60-
expect(getOrdinalNumber(22)).toEqual("22nd");
61-
});
62-
63-
// Case 8: Identify the ordinal number for 23
64-
// When the number is 23,
65-
// Then the function should return "23rd"
66-
67-
test("should return '23rd' for 23", () => {
68-
expect(getOrdinalNumber(23)).toEqual("23rd");
69-
});

0 commit comments

Comments
 (0)