Skip to content

Commit 7b64029

Browse files
committed
created tests and function for get-ordinal-number even in large numbers
1 parent ea7e00c commit 7b64029

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

Sprint-3/2-practice-tdd/count.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ function countChar(stringOfCharacters, findCharacter) {
66
count += 1;
77
}
88
}
9-
console.log(arrayOfCharacters);
10-
console.log(count);
9+
//console.log(arrayOfCharacters);
10+
//console.log(count);
1111
return count;
1212
}
1313

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const stringNum = String(num);
3+
const paddedLast2Digit = stringNum.padStart(2, "0").slice(-2);
4+
const numberValue = Number(paddedLast2Digit);
5+
6+
if (numberValue >= 11 && numberValue <= 13) {
7+
return `${stringNum}th`;
8+
}
9+
if (stringNum.endsWith("1")) {
10+
return `${stringNum}st`;
11+
}
12+
if (stringNum.endsWith("2")) {
13+
return `${stringNum}nd`;
14+
}
15+
if (stringNum.endsWith("3")) {
16+
return `${stringNum}rd`;
17+
}
18+
return `${stringNum}th`;
319
}
420

521
module.exports = getOrdinalNumber;

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,51 @@ const getOrdinalNumber = require("./get-ordinal-number");
1111
test("should return '1st' for 1", () => {
1212
expect(getOrdinalNumber(1)).toEqual("1st");
1313
});
14+
test("should return '2nd' for 2", () => {
15+
expect(getOrdinalNumber(2)).toEqual("2nd");
16+
});
17+
test("should return '3rd' for 3", () => {
18+
expect(getOrdinalNumber(3)).toEqual("3rd");
19+
});
20+
test("should return '4th' for 4", () => {
21+
expect(getOrdinalNumber(4)).toEqual("4th");
22+
});
23+
test("should return '11th' for 11", () => {
24+
expect(getOrdinalNumber(11)).toEqual("11th");
25+
});
26+
test("should return '12th' for 12", () => {
27+
expect(getOrdinalNumber(12)).toEqual("12th");
28+
});
29+
test("should return '13th' for 13", () => {
30+
expect(getOrdinalNumber(13)).toEqual("13th");
31+
});
32+
test("should return '21st' for 21", () => {
33+
expect(getOrdinalNumber(21)).toEqual("21st");
34+
});
35+
test("should return '22nd' for 22", () => {
36+
expect(getOrdinalNumber(22)).toEqual("22nd");
37+
});
38+
test("should return '23rd' for 23", () => {
39+
expect(getOrdinalNumber(23)).toEqual("23rd");
40+
});
41+
test("should return '24th' for 24", () => {
42+
expect(getOrdinalNumber(24)).toEqual("24th");
43+
});
44+
test("should return '111th' for 111", () => {
45+
expect(getOrdinalNumber(111)).toEqual("111th");
46+
});
47+
test("should return '112th' for 112", () => {
48+
expect(getOrdinalNumber(112)).toEqual("112th");
49+
});
50+
test("should return '113th' for 113", () => {
51+
expect(getOrdinalNumber(113)).toEqual("113th");
52+
});
53+
test("should return '121st' for 121", () => {
54+
expect(getOrdinalNumber(121)).toEqual("121st");
55+
});
56+
test("should return '1000011th' for 1000011", () => {
57+
expect(getOrdinalNumber(1000011)).toEqual("1000011th");
58+
});
59+
test("should return '1000002nd' for 1000002", () => {
60+
expect(getOrdinalNumber(1000002)).toEqual("1000002nd");
61+
});

0 commit comments

Comments
 (0)