Skip to content

Commit 407bf05

Browse files
committed
more testing scenarios added
1 parent 566278d commit 407bf05

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function getOrdinalNumber(num) {
1515
return num + "rd";
1616
default:
1717
return num + "th";
18+
1819
}
1920
}
2021

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,48 @@
11
const getOrdinalNumber = require("./get-ordinal-number");
2+
3+
24
// In this week's prep, we started implementing getOrdinalNumber
35

46
// continue testing and implementing getOrdinalNumber for additional cases
57
// Write your tests using Jest - remember to run your tests often for continual feedback
68

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", () => {
9+
// Case 1: Numbers ending in 1 (but not 11)
10+
test("append 'st' to numbers ending in 1, except those ending in 11", () => {
1211
expect(getOrdinalNumber(1)).toEqual("1st");
12+
expect(getOrdinalNumber(21)).toEqual("21st");
13+
expect(getOrdinalNumber(101)).toEqual("101st");
14+
expect(getOrdinalNumber(111)).toEqual("111th"); // exception
15+
});
16+
17+
// Case 2: Numbers ending in 2 (but not 12)
18+
test("append 'nd' to numbers ending in 2, except those ending in 12", () => {
19+
expect(getOrdinalNumber(2)).toEqual("2nd");
20+
expect(getOrdinalNumber(22)).toEqual("22nd");
21+
expect(getOrdinalNumber(102)).toEqual("102nd");
22+
expect(getOrdinalNumber(112)).toEqual("112th"); // exception
1323
});
14-
test("should return 'th' for 11, 12, 13", () => {
15-
expect(getOrdinalNumber(11)).toBe("11th");
16-
expect(getOrdinalNumber(12)).toBe("12th");
17-
expect(getOrdinalNumber(13)).toBe("13th");
24+
// Case 3: Numbers ending in 3 (but not 13)
25+
test("append 'rd' to numbers ending in 3, except those ending in 13", () => {
26+
expect(getOrdinalNumber(3)).toEqual("3rd");
27+
expect(getOrdinalNumber(23)).toEqual("23rd");
28+
expect(getOrdinalNumber(103)).toEqual("103rd");
29+
expect(getOrdinalNumber(113)).toEqual("113th"); // exception
1830
});
19-
test("should return correct ordinal for numbers > 20", () => {
20-
expect(getOrdinalNumber(21)).toBe("21st");
21-
expect(getOrdinalNumber(22)).toBe("22nd");
22-
expect(getOrdinalNumber(23)).toBe("23rd");
23-
});
31+
// Case 4: Numbers ending in 4–9 or exceptions (11, 12, 13)
32+
test("append 'th' to numbers ending in 4–9 or ending with 11, 12, or 13", () => {
33+
expect(getOrdinalNumber(4)).toEqual("4th");
34+
expect(getOrdinalNumber(9)).toEqual("9th");
35+
expect(getOrdinalNumber(11)).toEqual("11th");
36+
expect(getOrdinalNumber(12)).toEqual("12th");
37+
expect(getOrdinalNumber(13)).toEqual("13th");
38+
});
39+
// Optional: edge cases
40+
test("should handle 0 and large numbers correctly", () => {
41+
expect(getOrdinalNumber(0)).toBe("0th");
42+
expect(getOrdinalNumber(1000001)).toBe("1000001st"); // ends with 1, not teen
43+
expect(getOrdinalNumber(1000012)).toBe("1000012th"); // ends with 12 -> teen
44+
45+
});
46+
47+
48+

0 commit comments

Comments
 (0)