Skip to content

Commit e513388

Browse files
Implement nd suffix
1 parent 7add368 commit e513388

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
export function getOrdinalNumber(num) {
2-
return `${num}st`;
2+
const lastTwoDigits = num % 100;
3+
const lastDigits = num % 10;
4+
5+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
6+
return `${num}th`;
7+
} else if (lastDigits === 1) {
8+
return `${num}st`;
9+
} else if (lastDigits === 2) {
10+
return `${num}nd`;
11+
} else if (lastDigits === 3) {
12+
return `${num}rd`;
13+
} else {
14+
return `${num}th`;
15+
}
316
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
1818
expect(getOrdinalNumber(21)).toEqual("21st");
1919
expect(getOrdinalNumber(131)).toEqual("131st");
2020
});
21+
22+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
23+
expect(getOrdinalNumber(2)).toEqual("2nd");
24+
expect(getOrdinalNumber(22)).toEqual("22nd");
25+
expect(getOrdinalNumber(112)).toEqual("112th");
26+
});

0 commit comments

Comments
 (0)