Skip to content

Commit 354b5ac

Browse files
author
Payman IB
committed
Refactor getOrdinalNumber function for clarity and improve test cases for better coverage
1 parent 5ee3f6b commit 354b5ac

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
function getOrdinalNumber(num) {
22
const suffixes = ["th", "st", "nd", "rd"];
3-
const date = num % 100;
3+
const lastTwoDigits = num % 100;
44

5-
if (date >= 11 && date <= 13) {
5+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
66
return num + "th";
77
}
8-
const suffix = suffixes[(date % 10)] || "th";
8+
const suffix = suffixes[(lastTwoDigits % 10)] || "th";
99
return num + suffix;
1010
}
1111

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ test("append 'rd' to numbers ending in 3, except those ending in 13", () => {
2424
expect( getOrdinalNumber(33) ).toEqual("33rd");
2525
expect( getOrdinalNumber(133) ).toEqual("133rd");
2626
});
27-
test("append 'th' to numbers ending in 4, except those ending in 14", () => {
27+
test("append 'th' to numbers ending in 4", () => {
2828
expect(getOrdinalNumber(4)).toEqual("4th");
2929
expect( getOrdinalNumber(24) ).toEqual("24th");
3030
expect( getOrdinalNumber(134) ).toEqual("134th");
3131
});
3232
test("should return '11th' for 11", () => {
3333
expect(getOrdinalNumber(11)).toEqual("11th");
34-
});
34+
});
35+
test("append 'th' to numbers which are not ending in 1, 2, 3", () => {
36+
expect(getOrdinalNumber(5)).toEqual("5th");
37+
expect( getOrdinalNumber(29) ).toEqual("29th");
38+
expect( getOrdinalNumber(138) ).toEqual("138th");
39+
});

0 commit comments

Comments
 (0)