Skip to content
Open
12 changes: 11 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
function countChar(stringOfCharacters, findCharacter) {
return 5

let count = 0;

for (i = 0; i < stringOfCharacters.length; i++) {
if (stringOfCharacters[i] === findCharacter) {
count += 1;
}
}

return count;
}

module.exports = countChar;

32 changes: 26 additions & 6 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,41 @@ const countChar = require("./count");
// When the countChar function is called with these inputs,
// Then it should:

// Scenario: No Occurrences
// Given the input string `str`,
// 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 no occurrences if no found character", () => {
const str = "jhhfdd";
const char = "r";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Scenario: Multiple Occurrences
// Given the input string `str`,
// And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'),
// When the function is called with these inputs,
// Then it should correctly count occurrences of `char`.

test("should count multiple occurrences of a character", () => {
const str = "aaaaa";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(5);
});

// Scenario: No Occurrences
// Given the input string `str`,
// 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.
// case 2
test("should return 1 when the character appears only once", () => {
expect(countChar("hello", "h")).toEqual(1);
});

// case 3
test("Should return 0 for empty string", () => {
expect(countChar("", "a")).toEqual(0)
})

// case 4
test("should be case sensitive", () => {
expect(countChar("aA", "a")).toEqual(1);
});
16 changes: 15 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,19 @@
function getOrdinalNumber(num) {
return "1st";

const lastTwoDigits = num % 100;
const lastDigit = num % 10


if (lastTwoDigits === 11 || lastTwoDigits === 12 || lastTwoDigits === 13) {
return num + "th";
}


if(lastDigit === 1) return num + "st";
if(lastDigit === 2) return num + "nd";
if(lastDigit === 3) return num + "rd";

return num + "th"
}

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

// Caso 2: number ending in 2 (nd) - but no 12
test("should append 'nd' for numbers ending with 2, except 12", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(102)).toEqual("102nd");
});

// case 3: numbers ending on 3 but no 13
test("Should append 'rd' for numbers ending with 3, except 13", () => {
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(33)).toEqual("33rd");
});

// case 4: numbers exception like 11, 12 and 13 append th
test("should append 'th' for the exceptions 11, 12, and 13", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
});

// case 5: rest of numbers
test("should append 'th' for number ending in 4, 5, 6, 7, 8, 9 or 0", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(6)).toEqual("6th");
expect(getOrdinalNumber(17)).toEqual("17th");
expect(getOrdinalNumber(18)).toEqual("18th");
expect(getOrdinalNumber(29)).toEqual("29th");
expect(getOrdinalNumber(30)).toEqual("30th");
});
30 changes: 28 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
function repeatStr() {
return "hellohellohello";
function repeatStr(str, count) {
if (count < 0) {
throw new Error("count cannot be negative 😭");
}

let result = "";
for (let i = 0; i < count; i++) {
result = result + str;
}
return result;
}

module.exports = repeatStr;


// Case: Manual inspection of the Error object
// We use try/catch to log the error message to the console
// to verify it's working as expected without breaking the test suite.
test("Should log the specific error message to the console", () => {
try {
repeatStr("hi", -3);
} catch (error) {
console.log("Verified Error Message ->", error.message);

// Check if the message is exactly what we defined
expect(error.message).toBe("count cannot be negative 😭");
}

// Also verify that it actually throws using Jest's built-in matcher
expect(() => repeatStr("hi", -3)).toThrow();
});
12 changes: 12 additions & 0 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const repeatStr = require("./repeat-str");
// When the repeatStr function is called with these inputs,
// Then it should:

// Clean Code example, Gherkin style (Given/When/Then)

// Case: handle multiple repetitions:
// Given a target string `str` and a positive integer `count` greater than 1,
// When the repeatStr function is called with these inputs,
Expand All @@ -20,13 +22,23 @@ 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 if count is 1", () => {
expect(repeatStr("singleStf", 1)).toBe("singleStf");
});


// 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 if the count is 0", () => {
expect(repeatStr("hello", 0)).toBe("");
});

// 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 if the count is negative", () => {
expect(() => repeatStr("hi", -3)).toThrow();
});