Skip to content

Commit 5e4a23a

Browse files
committed
implemented getOrdinalNumber and added tests
1 parent 552bb4c commit 5e4a23a

2 files changed

Lines changed: 78 additions & 5 deletions

File tree

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
// Validate input: must be a number (not a string, null or undefined) and not NaN
3+
if (typeof num !== "number" || Number.isNaN(num)) {
4+
throw new TypeError("Input must be a number");
5+
}
6+
7+
const abs = Math.abs(num);
8+
const lastTwoDigits = abs % 100;
9+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
10+
return `${num}th`;
11+
}
12+
const lastDigit = lastTwoDigits % 10;
13+
if (lastDigit === 1) {
14+
return `${num}st`;
15+
}
16+
if (lastDigit === 2) {
17+
return `${num}nd`;
18+
}
19+
if (lastDigit === 3) {
20+
return `${num}rd`;
21+
}
22+
return `${num}th`;
323
}
424

525
module.exports = getOrdinalNumber;

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

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,61 @@ const getOrdinalNumber = require("./get-ordinal-number");
1313
// Case 1: Numbers ending with 1 (but not 11)
1414
// When the number ends with 1, except those ending with 11,
1515
// Then the function should return a string by appending "st" to the number.
16-
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
17-
expect(getOrdinalNumber(1)).toEqual("1st");
18-
expect(getOrdinalNumber(21)).toEqual("21st");
19-
expect(getOrdinalNumber(131)).toEqual("131st");
16+
test("should append 'st' for numbers ending with 1, excluding 11", () => {
17+
expect(getOrdinalNumber(1)).toBe("1st");
18+
expect(getOrdinalNumber(21)).toBe("21st");
19+
expect(getOrdinalNumber(101)).toBe("101st");
20+
expect(getOrdinalNumber(131)).toBe("131st");
21+
});
22+
23+
test("should append 'nd' for numbers ending with 2, excluding 12", () => {
24+
expect(getOrdinalNumber(2)).toBe("2nd");
25+
expect(getOrdinalNumber(22)).toBe("22nd");
26+
expect(getOrdinalNumber(132)).toBe("132nd");
27+
});
28+
29+
test("should append 'rd' for numbers ending with 3, excluding 13", () => {
30+
expect(getOrdinalNumber(3)).toBe("3rd");
31+
expect(getOrdinalNumber(23)).toBe("23rd");
32+
expect(getOrdinalNumber(133)).toBe("133rd");
33+
});
34+
35+
test("should append 'th' for numbers ending with 11, 12, or 13", () => {
36+
expect(getOrdinalNumber(11)).toBe("11th");
37+
expect(getOrdinalNumber(12)).toBe("12th");
38+
expect(getOrdinalNumber(13)).toBe("13th");
39+
expect(getOrdinalNumber(111)).toBe("111th");
40+
expect(getOrdinalNumber(112)).toBe("112th");
41+
expect(getOrdinalNumber(113)).toBe("113th");
42+
});
43+
44+
test("should append 'th' for numbers ending with 4-9 or 0", () => {
45+
expect(getOrdinalNumber(4)).toBe("4th");
46+
expect(getOrdinalNumber(5)).toBe("5th");
47+
expect(getOrdinalNumber(6)).toBe("6th");
48+
expect(getOrdinalNumber(7)).toBe("7th");
49+
expect(getOrdinalNumber(8)).toBe("8th");
50+
expect(getOrdinalNumber(9)).toBe("9th");
51+
expect(getOrdinalNumber(0)).toBe("0th");
52+
});
53+
54+
test("non-integer and negative numbers should be handled correctly", () => {
55+
expect(getOrdinalNumber(1.5)).toBe("1.5th");
56+
expect(getOrdinalNumber(-2.3)).toBe("-2.3th");
57+
expect(getOrdinalNumber(-11)).toBe("-11th");
58+
});
59+
60+
test("invalid input should throw an error", () => {
61+
expect(() => getOrdinalNumber("string")).toThrow("Input must be a number");
62+
expect(() => getOrdinalNumber(null)).toThrow("Input must be a number");
63+
expect(() => getOrdinalNumber(undefined)).toThrow("Input must be a number");
64+
});
65+
66+
test("NaN input should throw an error", () => {
67+
expect(() => getOrdinalNumber(NaN)).toThrow("Input must be a number");
68+
});
69+
70+
test("Infinity input should be handled correctly", () => {
71+
expect(getOrdinalNumber(Infinity)).toBe("Infinityth");
72+
expect(getOrdinalNumber(-Infinity)).toBe("-Infinityth");
2073
});

0 commit comments

Comments
 (0)