Skip to content

Commit effcc4b

Browse files
committed
Added and pass tests for countChar, getOrdinalNumber, and repeat functions
1 parent 8f3d6cf commit effcc4b

File tree

6 files changed

+116
-4
lines changed

6 files changed

+116
-4
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
// Ensure valid inputs
3+
if (
4+
typeof stringOfCharacters !== "string" ||
5+
typeof findCharacter !== "string"
6+
) {
7+
return 0;
8+
}
9+
10+
// Count occurrences
11+
let count = 0;
12+
for (let char of stringOfCharacters) {
13+
if (char === findCharacter) {
14+
count++;
15+
}
16+
}
17+
18+
return count;
319
}
420

521
module.exports = countChar;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ 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 when the character is not found", () => {
26+
const str = "hello";
27+
const char = "z";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
});
Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
// Ensure the input is a valid number
3+
if (typeof num !== "number" || isNaN(num)) {
4+
return "";
5+
}
6+
7+
const lastTwoDigits = num % 100;
8+
const lastDigit = num % 10;
9+
10+
// Handle special cases: 11th, 12th, 13th
11+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
12+
return `${num}th`;
13+
}
14+
15+
// Handle normal ordinal endings
16+
switch (lastDigit) {
17+
case 1:
18+
return `${num}st`;
19+
case 2:
20+
return `${num}nd`;
21+
case 3:
22+
return `${num}rd`;
23+
default:
24+
return `${num}th`;
25+
}
326
}
427

528
module.exports = getOrdinalNumber;

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,44 @@ const getOrdinalNumber = require("./get-ordinal-number");
88
// When the number is 1,
99
// Then the function should return "1st"
1010

11+
12+
// Case 1: Identify the ordinal number for 1
1113
test("should return '1st' for 1", () => {
1214
expect(getOrdinalNumber(1)).toEqual("1st");
1315
});
16+
17+
// Case 2: Identify the ordinal number for 2
18+
test("should return '2nd' for 2", () => {
19+
expect(getOrdinalNumber(2)).toEqual("2nd");
20+
});
21+
22+
// Case 3: Identify the ordinal number for 3
23+
test("should return '3rd' for 3", () => {
24+
expect(getOrdinalNumber(3)).toEqual("3rd");
25+
});
26+
27+
// Case 4: Identify the ordinal number for 4
28+
test("should return '4th' for 4", () => {
29+
expect(getOrdinalNumber(4)).toEqual("4th");
30+
});
31+
32+
// Case 5: Handle special cases 11, 12, 13
33+
test("should return '11th', '12th', '13th' for special cases", () => {
34+
expect(getOrdinalNumber(11)).toEqual("11th");
35+
expect(getOrdinalNumber(12)).toEqual("12th");
36+
expect(getOrdinalNumber(13)).toEqual("13th");
37+
});
38+
39+
// Case 6: Handle 21, 22, 23 correctly
40+
test("should handle 21st, 22nd, 23rd correctly", () => {
41+
expect(getOrdinalNumber(21)).toEqual("21st");
42+
expect(getOrdinalNumber(22)).toEqual("22nd");
43+
expect(getOrdinalNumber(23)).toEqual("23rd");
44+
});
45+
46+
// Case 7: Handle numbers ending with 0, 4–9
47+
test("should return 'th' for numbers ending with 0, 4–9", () => {
48+
expect(getOrdinalNumber(10)).toEqual("10th");
49+
expect(getOrdinalNumber(14)).toEqual("14th");
50+
expect(getOrdinalNumber(19)).toEqual("19th");
51+
});

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
function repeat() {
2-
return "hellohellohello";
1+
function repeat(str, count) {
2+
// Validate inputs
3+
if (typeof str !== "string") {
4+
throw new Error("First argument must be a string");
5+
}
6+
7+
if (typeof count !== "number" || count < 0) {
8+
throw new Error("Count must be a non-negative number");
9+
}
10+
11+
// Repeat string count times
12+
return str.repeat(count);
313
}
414

515
module.exports = repeat;

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,32 @@ test("should repeat the string count times", () => {
2020
// Given a target string str and a count equal to 1,
2121
// When the repeat function is called with these inputs,
2222
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
23+
test("should return the original string when count is 1", () => {
24+
const str = "hi";
25+
const count = 1;
26+
const repeatedStr = repeat(str, count);
27+
expect(repeatedStr).toEqual("hi");
28+
});
2329

2430
// case: Handle Count of 0:
2531
// Given a target string str and a count equal to 0,
2632
// When the repeat function is called with these inputs,
2733
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
34+
test("should return an empty string when count is 0", () => {
35+
const str = "test";
36+
const count = 0;
37+
const repeatedStr = repeat(str, count);
38+
expect(repeatedStr).toEqual("");
39+
});
2840

2941
// case: Negative Count:
3042
// Given a target string str and a negative integer count,
3143
// When the repeat function is called with these inputs,
3244
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
45+
test("should throw an error when count is negative", () => {
46+
const str = "error";
47+
const count = -2;
48+
expect(() => repeat(str, count)).toThrow(
49+
"Count must be a non-negative number"
50+
);
51+
});

0 commit comments

Comments
 (0)