Skip to content

Commit 87552c8

Browse files
committed
get-ordinal-number is done
1 parent 47c3136 commit 87552c8

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
function getOrdinalNumber(number) {
2-
if (number === 1) return "1st";
3-
if (number === 2) return "2nd";
4-
if (number === 3) return "3rd";
5-
return number + "th";
2+
if (number % 100 >= 11 && number % 100 <= 13) {
3+
return number + "th";
4+
} else {
5+
const lastdigit = number.toString().slice(-1);
6+
7+
if (lastdigit == 1) return number + "st";
8+
if (lastdigit == 2) return number + "nd";
9+
if (lastdigit == 3) return number + "rd";
10+
return number + "th";
11+
}
612
}
713

814
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
@@ -32,3 +32,31 @@ test("should return '3rd' for 3", () => {
3232
test("should return '4th' for 4", () => {
3333
expect(getOrdinalNumber(4)).toEqual("4th");
3434
});
35+
36+
// Case 9: Identify the ordinal number for 9
37+
// When the number is 9,
38+
// Then the function should return "4th"
39+
test("should return '9th' for 9", () => {
40+
expect(getOrdinalNumber(9)).toEqual("9th");
41+
});
42+
43+
// Case 10: Identify the ordinal number for 10
44+
// When the number is 10,
45+
// Then the function should return "4th"
46+
test("should return '10th' for 10", () => {
47+
expect(getOrdinalNumber(10)).toEqual("10th");
48+
});
49+
50+
// Case 11: Identify the ordinal number for 11
51+
// When the number is 11,
52+
// Then the function should return "4th"
53+
test("should return '11th' for 11", () => {
54+
expect(getOrdinalNumber(11)).toEqual("11th");
55+
});
56+
57+
// Case 23: Identify the ordinal number for 23
58+
// When the number is 23,
59+
// Then the function should return "33rd"
60+
test("should return '23rd' for 23", () => {
61+
expect(getOrdinalNumber(23)).toEqual("23rd");
62+
});

0 commit comments

Comments
 (0)