diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..237b7619c 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,14 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + + for (const letter of stringOfCharacters) { + if (letter === findCharacter) { + count++; + } + } + + return count; + } module.exports = countChar; diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf..7979ccab9 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,17 @@ 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 the character does not exist in the string", () => { + const str = "hello"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// Bonus Scenario: Mixed Case (Character searching is case-sensitive) +test("should be case-sensitive", () => { + const str = "Abba"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(1); // Only the lowercase 'a' is counted +}); \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db1..04a400b77 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,27 @@ + function getOrdinalNumber(num) { - return "1st"; + const s = num.toString(); + const lastDigit = num % 10; + const lastTwoDigits = num % 100; + + // Rule for 11th, 12th, 13th (The "Teens" exception) + if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { + return s + "th"; + } + + // Standard rules based on the last digit + switch (lastDigit) { + case 1: + return s + "st"; + case 2: + return s + "nd"; + case 3: + return s + "rd"; + default: + return s + "th"; + } } + + module.exports = getOrdinalNumber; diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js index adfa58560..5dcd858f4 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -13,8 +13,42 @@ const getOrdinalNumber = require("./get-ordinal-number"); // Case 1: Numbers ending with 1 (but not 11) // When the number ends with 1, except those ending with 11, // Then the function should return a string by appending "st" to the number. -test("should append 'st' for numbers ending with 1, except those ending with 11", () => { - expect(getOrdinalNumber(1)).toEqual("1st"); - expect(getOrdinalNumber(21)).toEqual("21st"); - expect(getOrdinalNumber(131)).toEqual("131st"); -}); + +describe("getOrdinalNumber", () => { + test("should append 'st' for numbers ending with 1, except those ending with 11", () => { + expect(getOrdinalNumber(1)).toEqual("1st"); + expect(getOrdinalNumber(21)).toEqual("21st"); + expect(getOrdinalNumber(131)).toEqual("131st"); + }); + + // Case 2: Numbers ending with 2 (but not 12) + test("should append 'nd' for numbers ending with 2, except those ending with 12", () => { + expect(getOrdinalNumber(2)).toBe("2nd"); + expect(getOrdinalNumber(42)).toBe("42nd"); + expect(getOrdinalNumber(122)).toBe("122nd"); + }); + + // Case 3: Numbers ending with 3 (but not 13) + test("should append 'rd' for numbers ending with 3, except those ending with 13", () => { + expect(getOrdinalNumber(3)).toBe("3rd"); + expect(getOrdinalNumber(53)).toBe("53rd"); + expect(getOrdinalNumber(1003)).toBe("1003rd"); + }); + + // Case 4: The Teen Exceptions (11, 12, 13) + test("should append 'th' for numbers ending in 11, 12, or 13", () => { + expect(getOrdinalNumber(11)).toBe("11th"); + expect(getOrdinalNumber(12)).toBe("12th"); + expect(getOrdinalNumber(13)).toBe("13th"); + expect(getOrdinalNumber(111)).toBe("111th"); // Edge case: triple digits + }); + + // Case 5: All other numbers (Ending in 0, 4, 5, 6, 7, 8, 9) + test("should append 'th' for numbers ending in 0, or 4 through 9", () => { + expect(getOrdinalNumber(0)).toBe("0th"); + expect(getOrdinalNumber(4)).toBe("4th"); + expect(getOrdinalNumber(9)).toBe("9th"); + expect(getOrdinalNumber(10)).toBe("10th"); + expect(getOrdinalNumber(20)).toBe("20th"); + }); +}); \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 3838c7b00..cbb4887de 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,5 +1,11 @@ -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; diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index a3fc1196c..a2c3b5356 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -1,32 +1,39 @@ // Implement a function repeatStr const repeatStr = require("./repeat-str"); -// Given a target string `str` and a positive integer `count`, -// When the repeatStr function is called with these inputs, -// Then it should: -// 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, -// Then it should return a string that contains the original `str` repeated `count` times. -test("should repeat the string count times", () => { - const str = "hello"; - const count = 3; - const repeatedStr = repeatStr(str, count); - expect(repeatedStr).toEqual("hellohellohello"); -}); +describe("repeatStr", () => { + // Case: handle multiple repetitions + test("should repeat the string count times", () => { + const str = "hello"; + const count = 3; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("hellohellohello"); + }); + + // Case: handle count of 1 + test("should return the original string when count is 1", () => { + const str = "apple"; + const count = 1; + expect(repeatStr(str, count)).toEqual("apple"); + }); -// Case: handle count of 1: -// 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. + // Case: Handle count of 0 + test("should return an empty string when count is 0", () => { + const str = "ghost"; + const count = 0; + expect(repeatStr(str, count)).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. + // Case: Handle negative count + test("should throw an error when count is negative", () => { + const str = "error"; + const count = -1; + + // We wrap the call in a function so Jest can catch the error + expect(() => { + repeatStr(str, count); + }).toThrow("Count must be a non-negative integer"); + }); +}); -// 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.