Skip to content
8 changes: 7 additions & 1 deletion 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 (const character of stringOfCharacters) {
if(character === findCharacter) {
count++;
}
}
return count;
}

module.exports = countChar;
59 changes: 59 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,62 @@ test("should count multiple occurrences of a character", () => {
// And a character `char` that does not exist within `str`.
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of `char` were found.
test("should return 0 when character does not appear in string", () => {
expect(countChar("hello", "x")).toEqual(0);
expect(countChar("banana", "z")).toEqual(0);
// empty string
expect(countChar("", "a")).toEqual(0);
});

// Scenario: Single Occurence
// Given a string where `char` appears exactly once,
// When countChar is called,
// Then it should return 1.
test("should return 1 when character appears exactly once", () => {
expect(countChar("hello", "h")).toEqual(1);
expect(countChar("test", "e")).toEqual(1);
expect(countChar("xyz", "y")).toEqual(1);
});

// Scenario: Case Sensitivity
// Given a string with mixed case letters,
// When countChar is called with a specific case,
// Then it should count only exact matches (case-sensitive).
test("should be case-sensitive when counting characters", () => {
expect(countChar("Hello", "H")).toEqual(1);
expect(countChar("Hello", "h")).toEqual(0);
expect(countChar("AAAaaa", "A")).toEqual(3);
expect(countChar("AAAaaa", "a")).toEqual(3);
});

// Scenario: Special Characters/Symbols
// Given a string containing punctuation or emojis,
// When counting a special character,
// Then it should correctly identify and count it.
test("should count special characters and emojis", () => {
expect(countChar("hello!!!", "!")).toEqual(3);
expect(countChar("smile 😊😊😊", "😊")).toEqual(3);
});

// Scenario: Longer Mixed String
// Given a longer string with various characters and spaces,
// When counting different characters,
// Then it should return the correct count for each.
test("should count correctly in longer mixed string", () => {
const str = "i am writing some jest tests right now";
// some, now
expect(countChar(str, "o")).toEqual(2);
// some, jest, tests
expect(countChar(str, "e")).toEqual(3);
// spaces
expect(countChar(str, " ")).toEqual(7);
});

// Scenario: Single Character String
// Given a string containing only one character,
// When countChar is called,
// Then it should correctly handle matching or non-matching cases.
test("should handle single character strings", () => {
expect(countChar("a", "a")).toEqual(1);
expect(countChar("b", "a")).toEqual(0);
});
20 changes: 19 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,23 @@
function getOrdinalNumber(num) {
return "1st";
// Last digit determines st/nd/rd for most numbers
const lastDigit = num % 10;
// Last two digits needed to handle special,
// teen cases (11th, 12th, 13th)
const lastTwo = num % 100;

// Special rule: numbers ending in 11, 12 or 13 always use "th"
if (lastTwo === 11 || lastTwo === 12 || lastTwo === 13) {
return num + "th";
}

// Ending in 1 → "st"
if (lastDigit === 1) return num + "st";
// Ending in 2 → "nd"
if (lastDigit === 2) return num + "nd";
// Ending in 3 → "rd"
if (lastDigit === 3) return num + "rd";
// All other endings (0, 4-9 and anything after teen check) → "th"
return num + "th";
}

module.exports = getOrdinalNumber;
44 changes: 44 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,47 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});

// Case 2: Numbers ending with 2 (except 12)
// Given the number ends with 2 but not 12,
// When getOrdinalNumber is called,
// Then it should append "nd".
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(102)).toEqual("102nd");
});

// Case 3: Numbers ending with 3 (except 13)
// Given the number ends with 3 but not 13,
// When getOrdinalNumber is called,
// Then it should append "rd".
test("should append 'rd' for numbers ending with 3 except those ending with 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(103)).toEqual("103rd");
});

// Case 4: Numbers ending with 4-9, 0, or 11-19
// Given the number ends with 4-9, 0, ot 11-19,
// When getOrdinalNumber is called,
// Then it should append "th".
test("should append 'th' for numbers ending with 4-9, 0, or 11-19", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(5)).toEqual("5th");
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(20)).toEqual("20th");
});

// Case 5: Large numbers
// Give a very large positive number,
// When getOrdinalNumber is called,
// Then it should still append the correct suffix based on the last digits.
test("should handle large numbers correctly", () => {
expect(getOrdinalNumber(101)).toEqual("101st");
expect(getOrdinalNumber(112)).toEqual("112th");
expect(getOrdinalNumber(1000)).toEqual("1000th");
});
25 changes: 23 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
function repeatStr() {
return "hellohellohello";
function repeatStr(str, count) {
// Reject negative counts - not valid for repetition
if (count < 0) {
throw new Error("Count cannot be negative");
}

// If count is 0, return empty string immediately
if (count === 0) {
return "";
}

// For count = 1, just return the original string
if (count === 1) {
return str;
}

// For count > 1: build the repeated string using a loop
let result = "";
for (let i = 0; i < count; i++) {
result += str;
}

return result;
}

module.exports = repeatStr;
24 changes: 24 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,37 @@ 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", () => {
expect(repeatStr("hello", 1)).toEqual("hello");
expect(repeatStr("a", 1)).toEqual("a");
expect(repeatStr("", 1)).toEqual("");
});

// 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 empty string when count is 0", () => {
expect(repeatStr("hello", 0)).toEqual("");
expect(repeatStr("anything", 0)).toEqual("");
expect(repeatStr("", 0)).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 error when count is negative", () => {
expect(() => repeatStr("hello", -1)).toThrow();
expect(() => repeatStr("test", -5)).toThrow();
expect(() => repeatStr("", -3)).toThrow();
});

// Case: Handle positive count:
// Given an empty string `str`and a positive integer `count`,
// When the repeatStr function is called,
// Then it should return an empty string
test("should return empty string when input string is empty", () => {
expect(repeatStr("", 5)).toEqual("");
expect(repeatStr("", 1)).toEqual("");
});