Skip to content

Commit 9e894a7

Browse files
author
Payman IB
committed
Refactor getOrdinalNumber function and expand test cases for ordinal numbers
1 parent 92afa66 commit 9e894a7

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const suffixes = ["th", "st", "nd", "rd"];
3+
const v = num % 100;
4+
console.log(v);
5+
if (v >= 11 && v <= 13) {
6+
return num + "th";
7+
}
8+
const suffix = suffixes[(v % 10)] || "th";
9+
return num + suffix;
310
}
11+
console.log(getOrdinalNumber(52));
412

513
module.exports = getOrdinalNumber;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,17 @@ 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+
test("should return '24th' for 24", () => {
23+
expect(getOrdinalNumber(24)).toEqual("24th");
24+
});
25+
test("should return '11th' for 11", () => {
26+
expect(getOrdinalNumber(11)).toEqual("11th");
27+
});

0 commit comments

Comments
 (0)