Skip to content

Commit 992821e

Browse files
committed
practice tdd get ordinal number
1 parent 213865e commit 992821e

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastTwo = num % 100;
3+
const last = num % 10;
4+
5+
// Special exceptions: 11, 12, 13
6+
if (lastTwo === 11 || lastTwo === 12 || lastTwo === 13) {
7+
return `${num}th`;
8+
}
9+
10+
// Regular cases
11+
if (last === 1) return `${num}st`;
12+
if (last === 2) return `${num}nd`;
13+
if (last === 3) return `${num}rd`;
14+
15+
return `${num}th`;
316
}
417

518
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+
//identify ordinal for 2
16+
test("should return '2nd' for 2", () => {
17+
expect(getOrdinalNumber(2)).toEqual("2nd");
18+
});
19+
20+
//identify ordinal for 3
21+
test("should return '3rd' for 3", () => {
22+
expect(getOrdinalNumber(3)).toEqual("3rd");
23+
});
24+
25+
//identify ordinal for 4
26+
test("should return '4th' for 4", () => {
27+
expect(getOrdinalNumber(4)).toEqual("4th");
28+
});
29+
30+
//Special English Rule... 12, 13, 14 ending in "th"
31+
test("should return '11th' for 11", () => {
32+
expect(getOrdinalNumber(11)).toEqual("11th");
33+
});
34+
35+
test("should return '12th' for 12", () => {
36+
expect(getOrdinalNumber(12)).toEqual("12th");
37+
});
38+
39+
test("should return '13th' for 13", () => {
40+
expect(getOrdinalNumber(13)).toEqual("13th");
41+
});

0 commit comments

Comments
 (0)