Skip to content
10 changes: 8 additions & 2 deletions Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
}
let count = 0;
for(let char of stringOfCharacters){
if (char === findCharacter) {
count++;
}
}
return count;
}

module.exports = countChar;
41 changes: 41 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,47 @@ test("should count multiple occurrences of a character", () => {
const count = countChar(str, char);
expect(count).toEqual(5);
});
test ("should return 0 when the character does not exist in the string", () => {
const str = "exist";
const char = "z";
const count = countChar(str, char);
expect(count).toEqual(0);
});
test("should return 0 when the input string is empty", () => {
const str = "";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(0);
});
test("should return 0 when the input character is an empty string", () => {
const str = "test";
const char = "";
const count = countChar(str, char);
expect(count).toEqual(0);
});
test("should return 0 when both the input string and character are empty", () => {
const str = "";
const char = "";
const count = countChar(str, char);
expect(count).toEqual(0);
});
test("should be case sensitive when counting characters", () => {
const str = "AaAaA";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(2);
});
test("should count non-alphabet characters", () => {
const str = "12345!";
const char = "1";
const count = countChar(str, char);
expect(count).toEqual(1);
});
// Scenario: Empty String
// Given an empty input string `str`,
// And any character `char` (e.g., 'a'),
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of `char` were found in the empty string.

// Scenario: No Occurrences
// Given the input string `str`,
Expand Down
18 changes: 17 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
function getOrdinalNumber(num) {
return "1st";
const str = String(num);

const lastTwo = num % 100;

if (lastTwo === 11 || lastTwo === 12 || lastTwo === 13) {
return str + "th";
}
const lastDigit = num % 10;
if (lastDigit === 1) {
return str + "st";
} else if (lastDigit === 2) {
return str + "nd";
} else if (lastDigit === 3) {
return str + "rd";
} else {
return str + "th";
}
}

module.exports = getOrdinalNumber;
21 changes: 21 additions & 0 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,24 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(132)).toEqual("132nd");
});
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(23)).toEqual("23rd");
});
test("should append 'th' for numbers ending with 11, 12, or 13", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(111)).toEqual("111th");
expect(getOrdinalNumber(112)).toEqual("112th");
expect(getOrdinalNumber(113)).toEqual("113th");
});
test("should append 'th' for numbers not ending with 1, 2, or 3", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(10)).toEqual("10th");
});
7 changes: 5 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
function repeatStr() {
return "hellohellohello";
function repeatStr(str, count) {
if (count < 0) {
throw new Error("Count must be a non-negative integer");
}
return str.repeat(count);
}

module.exports = repeatStr;
17 changes: 17 additions & 0 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,30 @@ test("should repeat the string count times", () => {
// Given a target string `str` and a `count` equal to 1,
// When the repeatStr function is called with these inputs,
// Then it should return the original `str` without repetition.
test("should return the original string when count is 1", () => {
const str = "hello";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hello");
});

// Case: Handle count of 0:
// Given a target string `str` and a `count` equal to 0,
// When the repeatStr function is called with these inputs,
// Then it should return an empty string.
test("should return an empty string when count is 0", () => {
const str = "hello";
const count = 0;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("");
});

// Case: Handle negative count:
// Given a target string `str` and a negative integer `count`,
// When the repeatStr function is called with these inputs,
// Then it should throw an error, as negative counts are not valid.
test("should throw an error when count is negative", () => {
const str = "hello";
const count = -1;
expect(() => repeatStr(str, count)).toThrow("Count must be a non-negative integer");
});
Comment on lines +45 to +49
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can your implementation pass this test?

Copy link
Author

@Maryanne-K Maryanne-K Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it would because it is specifically checking for a negative count, which is an error.

@cjyuan Kindly check implemented corrections. Thank you for your help.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you run the test script to find out if your function can pass the test or if the test is prepared correctly?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, just checked, it fails, forgot to add the word integer as the expected substring and received messages were different.