Skip to content

Commit df91c9f

Browse files
cover ordinal suffix exceptions and edge cases
1 parent f5154ef commit df91c9f

2 files changed

Lines changed: 16 additions & 44 deletions

File tree

‎practice-tdd/get-ordinal-number.js‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
export function getOrdinalNumber(num) {
2-
if (num % 100 === 11) {
3-
throw new Error("Input must not ending with 11");
4-
}
5-
if (num % 10 === 1) {
2+
if (num % 10 === 1 && num % 100 != 11) {
63
return `${num}st`;
74
}
85

9-
if (num % 100 === 22) {
10-
throw new Error("Input must not ending with 22");
11-
}
12-
13-
if (num % 10 === 2) {
6+
if (num % 10 === 2 && num % 100 != 12) {
147
return `${num}nd`;
158
}
16-
if (num % 100 === 33) {
17-
throw new Error("Input must not ending with 33");
18-
}
199

20-
if (num % 10 === 3) {
10+
if (num % 10 === 3 && num % 100 != 13) {
2111
return `${num}rd`;
2212
}
2313

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { getOrdinalNumber } from "./get-ordinal-number.js";
2-
import { test, expect } from "@jest/globals";
32
// In this week's prep, we started implementing getOrdinalNumber.
43

54
// Continue testing and implementing getOrdinalNumber for additional cases.
@@ -12,13 +11,6 @@ import { test, expect } from "@jest/globals";
1211
// This approach improves coverage and makes our tests easier to maintain.
1312

1413
// Case 1: Numbers ending with 1 (but not 11)
15-
test("should not append 'st' for number ending with 11 ", () => {
16-
expect(() => getOrdinalNumber(11)).toThrow("Input must not ending with 11");
17-
expect(() => getOrdinalNumber(111)).toThrow("Input must not ending with 11");
18-
expect(() => getOrdinalNumber(45711)).toThrow(
19-
"Input must not ending with 11"
20-
);
21-
});
2214

2315
// When the number ends with 1, except those ending with 11,
2416
// Then the function should return a string by appending "st" to the number.
@@ -29,42 +21,32 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
2921
expect(getOrdinalNumber(241)).toEqual("241st");
3022
});
3123

32-
// Case 2: Numbers ending with 2 (but not 22)
33-
// When the number ends with 2, except those ending with 22,
24+
// Case 2: Numbers ending with 2 (but not 12)
25+
// When the number ends with 2, except those ending with 12,
3426
// Then the function should return a string by appending "nd" to the number.
35-
test("should append 'nd' for numbers ending with 2, except those ending with 22", () => {
27+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
3628
expect(getOrdinalNumber(2)).toEqual("2nd");
29+
expect(getOrdinalNumber(22)).toEqual("22nd");
3730
expect(getOrdinalNumber(32)).toEqual("32nd");
3831
expect(getOrdinalNumber(132)).toEqual("132nd");
3932
});
4033

41-
test("should not append 'nd' for number ending with 22 ", () => {
42-
expect(() => getOrdinalNumber(22)).toThrow("Input must not ending with 22");
43-
expect(() => getOrdinalNumber(122)).toThrow("Input must not ending with 22");
44-
expect(() => getOrdinalNumber(45722)).toThrow(
45-
"Input must not ending with 22"
46-
);
47-
});
48-
49-
// Case 3: Numbers ending with 3 (but not 33)
50-
test("should append 'rd' for numbers ending with 3, except those ending with 33", () => {
34+
// Case 3: Numbers ending with 3 (but not 13)
35+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
5136
expect(getOrdinalNumber(3)).toEqual("3rd");
52-
expect(getOrdinalNumber(13)).toEqual("13rd");
37+
expect(getOrdinalNumber(33)).toEqual("33rd");
5338
expect(getOrdinalNumber(123)).toEqual("123rd");
5439
});
5540

56-
test("should not append 'rd' for number ending with 33 ", () => {
57-
expect(() => getOrdinalNumber(33)).toThrow("Input must not ending with 33");
58-
expect(() => getOrdinalNumber(133)).toThrow("Input must not ending with 33");
59-
expect(() => getOrdinalNumber(45733)).toThrow(
60-
"Input must not ending with 33"
61-
);
62-
});
63-
6441
// Case 3: Numbers ending in other digits
6542
test("should append 'th' for numbers ending in other digits", () => {
66-
expect(getOrdinalNumber(4)).toBe("4th");
43+
expect(getOrdinalNumber(11)).toBe("11th");
44+
expect(getOrdinalNumber(12)).toBe("12th");
45+
expect(getOrdinalNumber(13)).toBe("13th");
6746
expect(getOrdinalNumber(5)).toBe("5th");
6847
expect(getOrdinalNumber(10)).toBe("10th");
6948
expect(getOrdinalNumber(20)).toBe("20th");
49+
expect(getOrdinalNumber(111)).toBe("111th");
50+
expect(getOrdinalNumber(113)).toBe("113th");
51+
expect(getOrdinalNumber(112)).toBe("112th");
7052
});

0 commit comments

Comments
 (0)