Skip to content

Commit 4377262

Browse files
Adding jest test for numbers ending in 2
1 parent ea81dae commit 4377262

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastDigit = num % 10;
3+
const lastTwoDigits = num % 100;
4+
5+
if (lastDigit === 1 && lastTwoDigits !== 11) return num + "st";
6+
if (lastDigit === 2 && lastTwoDigits !== 12) return num + "nd";
7+
if (lastDigit === 3 && lastTwoDigits !== 13) return num + "rd";
8+
9+
return num + "th";
310
}
411

512
module.exports = getOrdinalNumber;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ 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+
17+
// Case 1: Numbers ending in 1.
1618
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
1719
expect(getOrdinalNumber(1)).toEqual("1st");
1820
expect(getOrdinalNumber(21)).toEqual("21st");
1921
expect(getOrdinalNumber(131)).toEqual("131st");
2022
});
23+
24+
// Case 2: Numbers ending in 2 (but not 12).
25+
test("appends 'nd' for numbers ending in 2 except 12", () => {
26+
expect(getOrdinalNumber(2)).toEqual("2nd");
27+
expect(getOrdinalNumber(22)).toEqual("22nd");
28+
});

0 commit comments

Comments
 (0)