Skip to content

Commit cb9b03e

Browse files
committed
implement getOrdinalNumber function and add tests
1 parent 728aaee commit cb9b03e

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const remainderTen = num % 10;
3+
const remainderHundred = num % 100;
4+
5+
if (remainderHundred >= 11 && remainderHundred <= 13) {
6+
return `${num}th`;
7+
}
8+
9+
switch (remainderTen) {
10+
case 1:
11+
return `${num}st`;
12+
case 2:
13+
return `${num}nd`;
14+
case 3:
15+
return `${num}rd`;
16+
default:
17+
return `${num}th`;
18+
}
319
}
420

521
module.exports = getOrdinalNumber;

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,31 @@ const getOrdinalNumber = require("./get-ordinal-number");
1111
test("should return '1st' for 1", () => {
1212
expect(getOrdinalNumber(1)).toEqual("1st");
1313
});
14+
15+
test("should return '2nd' for 2", () => {
16+
expect(getOrdinalNumber(2)).toEqual("2nd");
17+
});
18+
19+
test("should return '3rd' for 3", () => {
20+
expect(getOrdinalNumber(3)).toEqual("3rd");
21+
});
22+
23+
test("should return '4th' for 4", () => {
24+
expect(getOrdinalNumber(4)).toEqual("4th");
25+
});
26+
27+
test("should return '11th' for 11", () => {
28+
expect(getOrdinalNumber(11)).toEqual("11th");
29+
});
30+
31+
test("should return '12th' for 12", () => {
32+
expect(getOrdinalNumber(12)).toEqual("12th");
33+
});
34+
35+
test("should return '13th' for 13", () => {
36+
expect(getOrdinalNumber(13)).toEqual("13th");
37+
});
38+
39+
test("should return '111th' for 111", () => {
40+
expect(getOrdinalNumber(111)).toEqual("111th");
41+
});

0 commit comments

Comments
 (0)