Skip to content

Commit b48a37b

Browse files
committed
Completed get-ordinal-number
1 parent 447094d commit b48a37b

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastdigit = num % 10;
3+
if (lastdigit === 1) {
4+
return `${num}st`;
5+
}
6+
7+
if (lastdigit === 2) {
8+
return `${num}nd`;
9+
}
10+
11+
if (lastdigit === 3) {
12+
return `${num}rd`;
13+
} else {
14+
return `${num}th`;
15+
}
316
}
417

518
module.exports = getOrdinalNumber;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,15 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
1818
expect(getOrdinalNumber(21)).toEqual("21st");
1919
expect(getOrdinalNumber(131)).toEqual("131st");
2020
});
21+
22+
test("should append 'nd' for numbers ending with 2", () => {
23+
expect(getOrdinalNumber(2)).toEqual("2nd");
24+
expect(getOrdinalNumber(22)).toEqual("22nd");
25+
expect(getOrdinalNumber(132)).toEqual("132nd");
26+
});
27+
28+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
29+
expect(getOrdinalNumber(3)).toEqual("3rd");
30+
expect(getOrdinalNumber(33)).toEqual("33rd");
31+
expect(getOrdinalNumber(133)).toEqual("133rd");
32+
});

0 commit comments

Comments
 (0)