Skip to content

Commit 00f6f15

Browse files
committed
Implement getOrdinalNumber with tests
1 parent 514a13e commit 00f6f15

2 files changed

Lines changed: 58 additions & 3 deletions

File tree

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
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+
if (lastDigit === 1) {
10+
return `${num}st`;
11+
} else if (lastDigit === 2) {
12+
return `${num}nd`;
13+
} else if (lastDigit === 3) {
14+
return `${num}rd`;
15+
} else {
16+
return `${num}th`;
17+
}
318
}
419

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

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const getOrdinalNumber = require("./get-ordinal-number");
22
// In this week's prep, we started implementing getOrdinalNumber.
33

44
// Continue testing and implementing getOrdinalNumber for additional cases.
5-
// Write your tests using Jest remember to run your tests often for continual feedback.
5+
// Write your tests using Jest - remember to run your tests often for continual feedback.
66

77
// To ensure thorough testing, we need broad scenarios that cover all possible cases.
88
// Listing individual values, however, can quickly lead to an unmanageable number of test cases.
@@ -18,3 +18,43 @@ 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 with 2 (but not 12)
23+
// When the number ends with 2, except those ending with 12,
24+
// Then the function should return a string by appending "nd" to the number.
25+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
26+
expect(getOrdinalNumber(2)).toEqual("2nd");
27+
expect(getOrdinalNumber(22)).toEqual("22nd");
28+
expect(getOrdinalNumber(132)).toEqual("132nd");
29+
});
30+
31+
// Case 3: Numbers ending with 3 (but not 13)
32+
// When the number ends with 3, except those ending with 13,
33+
// Then the function should return a string by appending "rd" to the number.
34+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
35+
expect(getOrdinalNumber(3)).toEqual("3rd");
36+
expect(getOrdinalNumber(23)).toEqual("23rd");
37+
expect(getOrdinalNumber(133)).toEqual("133rd");
38+
});
39+
40+
// Case 4: Numbers ending with 11, 12, or 13
41+
// When the number ends with 11, 12, or 13,
42+
// Then the function should return a string by appending "th" to the number.
43+
test("should append 'th' for numbers ending with 11, 12, or 13", () => {
44+
expect(getOrdinalNumber(11)).toEqual("11th");
45+
expect(getOrdinalNumber(12)).toEqual("12th");
46+
expect(getOrdinalNumber(13)).toEqual("13th");
47+
expect(getOrdinalNumber(111)).toEqual("111th");
48+
expect(getOrdinalNumber(112)).toEqual("112th");
49+
expect(getOrdinalNumber(113)).toEqual("113th");
50+
});
51+
52+
// Case 5: All other numbers
53+
// When the number ends with 0, or 4 through 9,
54+
// Then the function should return a string by appending "th" to the number.
55+
test("should append 'th' for all other numbers", () => {
56+
expect(getOrdinalNumber(4)).toEqual("4th");
57+
expect(getOrdinalNumber(10)).toEqual("10th");
58+
expect(getOrdinalNumber(100)).toEqual("100th");
59+
expect(getOrdinalNumber(0)).toEqual("0th");
60+
});

0 commit comments

Comments
 (0)