Skip to content

Commit f04aba1

Browse files
committed
made the function more generic to cover all numbers and added more tests to check that different numbers work
1 parent 752f7ae commit f04aba1

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed
Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
function getOrdinalNumber(num) {
2-
if (num === 1) {
3-
return "1st";
4-
} else if (num === 2) {
5-
return "2nd";
6-
} else if (num === 3) {
7-
console.log(num);
8-
return "3rd";
9-
} else if (num > 3 || num < 21) {
10-
return `${num}th`;
2+
const lastDigit = num.toString()[num.toString().length - 1];
3+
4+
if (lastDigit === "1") {
5+
if (num === 11) {
6+
return `${num}th`;
7+
}
8+
9+
return `${num}st`;
10+
} else if (lastDigit === "2") {
11+
if (num.toString().length > 1) {
12+
const last2Digits = num.toString().slice(-2);
13+
14+
if (last2Digits === "12") {
15+
return `${num}th`;
16+
}
17+
}
18+
return `${num}nd`;
19+
} else if (lastDigit === "3") {
20+
if (num.toString().length > 1) {
21+
const last2Digits = num.toString().slice(-2);
22+
23+
if (last2Digits === "13") {
24+
return `${num}th`;
25+
}
26+
}
27+
return `${num}rd`;
1128
}
29+
30+
return `${num}th`;
1231
}
1332

1433
module.exports = getOrdinalNumber;

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,34 @@ const getOrdinalNumber = require("./get-ordinal-number");
88
// When the number is 1,
99
// Then the function should return "1st"
1010

11-
test("should return '1st' for 1", () => {
11+
test("should append 'st' to numbers with 1 at the end except for those ending with 11", () => {
1212
expect(getOrdinalNumber(1)).toEqual("1st");
13+
expect(getOrdinalNumber(21)).toEqual("21st");
14+
expect(getOrdinalNumber(101)).toEqual("101st");
15+
expect(getOrdinalNumber(151)).toEqual("151st");
16+
expect(getOrdinalNumber(2061)).toEqual("2061st");
1317
});
1418

15-
test("should return '2nd' for 2", () => {
19+
test("should append 'nd' to numbers with 2 at the end except for those ending with 12", () => {
1620
expect(getOrdinalNumber(2)).toEqual("2nd");
21+
expect(getOrdinalNumber(22)).toEqual("22nd");
22+
expect(getOrdinalNumber(342)).toEqual("342nd");
23+
expect(getOrdinalNumber(592)).toEqual("592nd");
24+
expect(getOrdinalNumber(1972)).toEqual("1972nd");
1725
});
1826

19-
test("should return '3rd' for 3", () => {
27+
test("should append 'rd' to numbers with 3 at the end except for those ending with 13", () => {
2028
expect(getOrdinalNumber(3)).toEqual("3rd");
29+
expect(getOrdinalNumber(33)).toEqual("33rd");
30+
expect(getOrdinalNumber(353)).toEqual("353rd");
31+
expect(getOrdinalNumber(93)).toEqual("93rd");
32+
expect(getOrdinalNumber(783)).toEqual("783rd");
2133
});
2234

23-
test("should return any number between 4 and 20 with a suffix of 'th' at end of number", () => {
35+
test("should append 'th' to all other numbers which do not end in 1,2,3,11,12 or 13", () => {
2436
expect(getOrdinalNumber(10)).toEqual("10th");
37+
expect(getOrdinalNumber(11)).toEqual("11th");
38+
expect(getOrdinalNumber(212)).toEqual("212th");
39+
expect(getOrdinalNumber(113)).toEqual("113th");
40+
expect(getOrdinalNumber(17)).toEqual("17th");
2541
});

0 commit comments

Comments
 (0)