Skip to content

Commit f200ee1

Browse files
committed
fix ordinal number logic, account for last two digits, not just 1
1 parent 0f0bd35 commit f200ee1

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
function getOrdinalNumber(num) {
2-
// 11 is a special case
3-
if (num === 11) {
4-
return "11th";
2+
const lastTwoDigits = num % 100;
3+
4+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
5+
return String(num) + "th";
56
}
7+
68
const lastDigit = num % 10;
79
switch (lastDigit) {
810
case 1:

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
1919
expect(getOrdinalNumber(131)).toEqual("131st");
2020
});
2121

22-
// Case 2: Numbers ending with 2
22+
// Case 2: Numbers ending with 2, except those ending with 12
2323
// When the number ends with 2
2424
// Then the function should return a string by appending "nd" to the number.
2525
test("should append 'nd' for numbers ending with 2", () => {
@@ -28,7 +28,7 @@ test("should append 'nd' for numbers ending with 2", () => {
2828
expect(getOrdinalNumber(132)).toEqual("132nd");
2929
});
3030

31-
// Case 3: Numbers ending with 3
31+
// Case 3: Numbers ending with 3, except those ending in 13
3232
// When the number ends with 3
3333
// Then the function should return a string by appending "rd" to the number.
3434
test("should append 'rd' for numbers ending with 3", () => {
@@ -37,7 +37,7 @@ test("should append 'rd' for numbers ending with 3", () => {
3737
expect(getOrdinalNumber(223)).toEqual("223rd");
3838
});
3939

40-
// Case 4: the general(ish) case
40+
// Case 4: the general case
4141
// When numbers end with 0, 4, 5, 6, 7, 8, 9
4242
// The function should return a string by appending "th" to the number
4343
test("should append 'th' for numbers ending with 0, 4, 5, 6, 7, 8, 9", () => {
@@ -48,11 +48,20 @@ test("should append 'th' for numbers ending with 0, 4, 5, 6, 7, 8, 9", () => {
4848
expect(getOrdinalNumber(57)).toEqual("57th");
4949
expect(getOrdinalNumber(78)).toEqual("78th");
5050
expect(getOrdinalNumber(89)).toEqual("89th");
51+
expect(getOrdinalNumber(189)).toEqual("189th");
5152
});
5253

53-
// Case 5: The special case of 11
54-
// When the number is 11
54+
// Case 5: The special cases when last two digits are 11, 12 13
55+
// When the last two digits are 11, 12, or 13
5556
// Then the function should return a string by appending "th" to the number.
56-
test("should append 'th' for 11", () => {
57+
test("should append 'th' for number whose last two digits are 11, 12, 13", () => {
5758
expect(getOrdinalNumber(11)).toEqual("11th");
59+
expect(getOrdinalNumber(12)).toEqual("12th");
60+
expect(getOrdinalNumber(13)).toEqual("13th");
61+
expect(getOrdinalNumber(111)).toEqual("111th");
62+
expect(getOrdinalNumber(112)).toEqual("112th");
63+
expect(getOrdinalNumber(113)).toEqual("113th");
64+
expect(getOrdinalNumber(211)).toEqual("211th");
65+
expect(getOrdinalNumber(212)).toEqual("212th");
66+
expect(getOrdinalNumber(313)).toEqual("313th");
5867
});

0 commit comments

Comments
 (0)