Skip to content

Commit ef0c3ec

Browse files
committed
feat(sprint-3): implement getOrdinalNumber function with TDD
1 parent 8d9a2ef commit ef0c3ec

File tree

2 files changed

+66
-9
lines changed

2 files changed

+66
-9
lines changed
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastDigit = num % 10;
3+
const lastTwoDigits = num % 100;
4+
5+
if (lastTwoDigits === 11 || lastTwoDigits === 12 || lastTwoDigits === 13) {
6+
return `${num}th`;
7+
}
8+
if (lastDigit === 1) return `${num}st`;
9+
if (lastDigit === 2) return `${num}nd`;
10+
if (lastDigit === 3) return `${num}rd`;
11+
return `${num}th`;
312
}
413

514
module.exports = getOrdinalNumber;
Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,61 @@
11
const getOrdinalNumber = require("./get-ordinal-number");
2-
// In this week's prep, we started implementing getOrdinalNumber
3-
4-
// continue testing and implementing getOrdinalNumber for additional cases
5-
// Write your tests using Jest - remember to run your tests often for continual feedback
6-
7-
// Case 1: Identify the ordinal number for 1
8-
// When the number is 1,
9-
// Then the function should return "1st"
102

113
test("should return '1st' for 1", () => {
124
expect(getOrdinalNumber(1)).toEqual("1st");
135
});
6+
7+
test("should return '2nd' for 2", () => {
8+
expect(getOrdinalNumber(2)).toEqual("2nd");
9+
});
10+
11+
test("should return '3rd' for 3", () => {
12+
expect(getOrdinalNumber(3)).toEqual("3rd");
13+
});
14+
15+
test("should handle numbers ending in 1 (except 11)", () => {
16+
expect(getOrdinalNumber(21)).toEqual("21st");
17+
expect(getOrdinalNumber(31)).toEqual("31st");
18+
expect(getOrdinalNumber(101)).toEqual("101st");
19+
});
20+
21+
test("should handle numbers ending in 2 (except 12)", () => {
22+
expect(getOrdinalNumber(22)).toEqual("22nd");
23+
expect(getOrdinalNumber(42)).toEqual("42nd");
24+
});
25+
26+
test("should handle numbers ending in 3 (except 13)", () => {
27+
expect(getOrdinalNumber(23)).toEqual("23rd");
28+
expect(getOrdinalNumber(33)).toEqual("33rd");
29+
});
30+
31+
test("should handle special cases 11, 12, 13", () => {
32+
expect(getOrdinalNumber(11)).toEqual("11th");
33+
expect(getOrdinalNumber(12)).toEqual("12th");
34+
expect(getOrdinalNumber(13)).toEqual("13th");
35+
});
36+
37+
test("should handle numbers ending in 4-9, 0", () => {
38+
expect(getOrdinalNumber(4)).toEqual("4th");
39+
expect(getOrdinalNumber(5)).toEqual("5th");
40+
expect(getOrdinalNumber(10)).toEqual("10th");
41+
});
42+
43+
test("should return '100th' for 100", () => {
44+
expect(getOrdinalNumber(100)).toEqual("100th");
45+
});
46+
47+
test("should return '24th' for 24", () => {
48+
expect(getOrdinalNumber(24)).toEqual("24th");
49+
});
50+
51+
test("should return '111th' for 111", () => {
52+
expect(getOrdinalNumber(111)).toEqual("111th");
53+
});
54+
55+
test("should return '112th' for 112", () => {
56+
expect(getOrdinalNumber(112)).toEqual("112th");
57+
});
58+
59+
test("should return '113th' for 113", () => {
60+
expect(getOrdinalNumber(113)).toEqual("113th");
61+
});

0 commit comments

Comments
 (0)