Skip to content

Commit 2dd8319

Browse files
committed
get-oridinal-number.js + tests completed
1 parent ba2b1c2 commit 2dd8319

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

‎practice-tdd/get-ordinal-number.js‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
export function getOrdinalNumber(num) {
2-
return "1st";
2+
const exceptions = [11, 12, 13];
3+
let strNum = num.toString();
4+
5+
for (let exception of exceptions) {
6+
if (strNum.endsWith(exception)) {
7+
return strNum + "th";
8+
}
9+
}
10+
11+
if (strNum.endsWith("1")) {
12+
return strNum + "st";
13+
} else if (strNum.endsWith("2")) {
14+
return strNum + "nd";
15+
} else if (strNum.endsWith("3")) {
16+
return strNum + "rd";
17+
} else {
18+
return strNum + "th";
19+
}
320
}

‎practice-tdd/get-ordinal-number.test.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,37 @@ import { getOrdinalNumber } from "./get-ordinal-number.js";
1515
// Then the function should return a string by appending "st" to the number.
1616
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
1717
expect(getOrdinalNumber(1)).toEqual("1st");
18+
expect(getOrdinalNumber(11)).toEqual("11th");
19+
expect(getOrdinalNumber(111)).toEqual("111th");
1820
expect(getOrdinalNumber(21)).toEqual("21st");
1921
expect(getOrdinalNumber(131)).toEqual("131st");
2022
});
23+
// Case 2: Numbers ending with 2 (but not 12)
24+
// When the number ends with 2, except those ending with 12,
25+
// Then the function should return a string by appending "nd" to the number.
26+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
27+
expect(getOrdinalNumber(2)).toEqual("2nd");
28+
expect(getOrdinalNumber(12)).toEqual("12th");
29+
expect(getOrdinalNumber(112)).toEqual("112th");
30+
expect(getOrdinalNumber(22)).toEqual("22nd");
31+
expect(getOrdinalNumber(132)).toEqual("132nd");
32+
});
33+
// Case 3: Numbers ending with 3 (but not 13)
34+
// When the number ends with 3, except those ending with 13,
35+
// Then the function should return a string by appending "rd" to the number.
36+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
37+
expect(getOrdinalNumber(3)).toEqual("3rd");
38+
expect(getOrdinalNumber(33)).toEqual("33rd");
39+
expect(getOrdinalNumber(13)).toEqual("13th");
40+
expect(getOrdinalNumber(113)).toEqual("113th");
41+
expect(getOrdinalNumber(133)).toEqual("133rd");
42+
});
43+
// Case 4: Numbers ending within the range of 4-9 (also ending with 0)
44+
// When the number ends within the range of 4-9 or ends with 0.
45+
// Then the function should return a string by appending "th" to the number.
46+
test("should append 'th' for numbers ending within 4-9, or ends with 0", () => {
47+
expect(getOrdinalNumber(5)).toEqual("5th");
48+
expect(getOrdinalNumber(37)).toEqual("37th");
49+
expect(getOrdinalNumber(139)).toEqual("139th");
50+
expect(getOrdinalNumber(100)).toEqual("100th");
51+
});

0 commit comments

Comments
 (0)