Skip to content

Commit 5813df0

Browse files
committed
Implement getOrdinalNumber function and add comprehensive Jest tests
1 parent 6c50af1 commit 5813df0

File tree

4 files changed

+89
-26
lines changed

4 files changed

+89
-26
lines changed

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
let counter=0;
3-
let index=stringOfCharacters.indexOf(findCharacter);
4-
while(index!== -1)
5-
{
6-
counter++;
7-
8-
index = stringOfCharacters.indexOf(findCharacter,index+1);
9-
}
10-
return counter;
2+
let counter = 0;
3+
let index = stringOfCharacters.indexOf(findCharacter);
4+
while (index !== -1) {
5+
counter++;
6+
7+
index = stringOfCharacters.indexOf(findCharacter, index + 1);
8+
}
9+
return counter;
1110
}
1211

1312
module.exports = countChar;
14-
function assertFunction(currentOutput,targetOutput)
15-
{
16-
console.assert(currentOutput===targetOutput,
13+
function assertFunction(currentOutput, targetOutput) {
14+
console.assert(
15+
currentOutput === targetOutput,
1716
`expect ${currentOutput} to equal ${targetOutput}`
1817
);
1918
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,34 @@ test("should count multiple occurrences of a character", () => {
2222
// And a character char that does not exist within the case-sensitive str,
2323
// When the function is called with these inputs,
2424
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
25-
test("should return 0 occurrences of b characters ",()=>{
26-
const str="Ahmad Hmedan";
27-
const char= "b";
28-
const count=countChar(str,char);
25+
test("should return 0 occurrences of b characters ", () => {
26+
const str = "Ahmad Hmedan";
27+
const char = "b";
28+
const count = countChar(str, char);
2929
expect(count).toEqual(0);
30-
})
30+
});
3131

32-
test("should return 1 occurrence of A characters",()=>{
32+
test("should return 1 occurrence of A characters", () => {
3333
const str = "Ahmad Hmedan";
3434
const char = "A";
3535
const count = countChar(str, char);
3636
expect(count).toEqual(1);
37-
})
38-
test("should return 2 occurrence of m characters",()=>{
37+
});
38+
test("should return 2 occurrence of m characters", () => {
3939
const str = "Ahmad Hmedan";
4040
const char = "m";
4141
const count = countChar(str, char);
4242
expect(count).toEqual(2);
43-
})
44-
test("should return occurrence of any characters in empty string",()=>{
43+
});
44+
test("should return occurrence of any characters in empty string", () => {
4545
const str = "";
4646
const char = "@";
4747
const count = countChar(str, char);
4848
expect(count).toEqual(0);
49-
})
50-
test("should return 1 occurrence of @ characters in empty string",()=>{
49+
});
50+
test("should return 1 occurrence of @ characters in empty string", () => {
5151
const str = "[email protected]";
5252
const char = "@";
5353
const count = countChar(str, char);
5454
expect(count).toEqual(1);
55-
})
55+
});
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+
if (num <= 0) return "Invalid Input";
3+
if (num % 100 === 11) return `${num}th`; // check the last two digit if it exception 11, 12 or 13
4+
if (num % 100 === 12) return `${num}th`;
5+
if (num % 100 === 13) return `${num}th`;
6+
const remainder = num % 10;
7+
switch (remainder) {
8+
case 1:
9+
return `${num}st`;
10+
11+
case 2:
12+
return `${num}nd`;
13+
case 3:
14+
return `${num}rd`;
15+
16+
default:
17+
return `${num}th`;
18+
}
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+
// Case 2: Identify the ordinal number for 22
15+
// When the number is 22,
16+
// Then the function should return 22nd"
17+
test("should return '22nd' for 22", () => {
18+
expect(getOrdinalNumber(22)).toEqual("22nd");
19+
});
20+
// Case 3: Identify the ordinal number for 73
21+
// When the number is 73,
22+
// Then the function should return 73rd"
23+
24+
test("should return '73rd' for 73", () => {
25+
expect(getOrdinalNumber(73)).toEqual("73rd");
26+
});
27+
28+
// Case 4: Identify the ordinal number for 99
29+
// When the number is 99,
30+
// Then the function should return 99th"
31+
32+
test("should return '99th' for 99", () => {
33+
expect(getOrdinalNumber(99)).toEqual("99th");
34+
});
35+
36+
// Case 5: Identify the ordinal number for number ends with 11,12, or 13
37+
// When the number is 111,
38+
// Then the function should return 111th"
39+
// When the number is 212,
40+
// Then the function should return 212th"
41+
// When the number is 413,
42+
// Then the function should return 413th"
43+
44+
test("should return '111th' for 111", () => {
45+
expect(getOrdinalNumber(111)).toEqual("111th");
46+
});
47+
test("should return '212th' for 212", () => {
48+
expect(getOrdinalNumber(212)).toEqual("212th");
49+
});
50+
test("should return '413th' for 413", () => {
51+
expect(getOrdinalNumber(413)).toEqual("413th");
52+
});
53+
54+
//case 6
55+
// when is the number is 0 or negative number
56+
test("should throw Invalid Input for negative numbers", () => {
57+
expect(getOrdinalNumber(-5)).toEqual("Invalid Input");
58+
});
59+
test("should throw Invalid Input for 0", () => {
60+
expect(getOrdinalNumber(0)).toEqual("Invalid Input");
61+
});

0 commit comments

Comments
 (0)