diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..8236f4f0b 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,15 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + + // Loop through each character in the string + for (let i = 0; i < stringOfCharacters.length; i++) { + // If the current character matches the character we're looking for + if (stringOfCharacters[i] === 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 42baf4b4b..79091d1cd 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,9 @@ test("should count multiple occurrences of a character", () => { // And a character char that does not exist within the case-sensitive str, // When the function is called with these inputs, // Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str. +test("should return 0 for no occurrences", () => { + const str = "hello"; + const char = "x"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); \ 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..689999a21 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,21 @@ function getOrdinalNumber(num) { - return "1st"; + // Handle special cases for teens (11th, 12th, 13th) + if (num % 100 >= 11 && num % 100 <= 13) { + return num + "th"; + } + + // Handle based on last digit + const lastDigit = num % 10; + + if (lastDigit === 1) { + return num + "st"; + } else if (lastDigit === 2) { + return num + "nd"; + } else if (lastDigit === 3) { + return num + "rd"; + } else { + return num + "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 dfe4b6091..f2b0402b9 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -11,3 +11,100 @@ const getOrdinalNumber = require("./get-ordinal-number"); test("should return '1st' for 1", () => { expect(getOrdinalNumber(1)).toEqual("1st"); }); + +// Case 2: Identify the ordinal number for 2 +// When the number is 2, +// Then the function should return "2nd" + +test("should return '2nd' for 2", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); +}); + +// Case 3: Identify the ordinal number for 3 +// When the number is 3, +// Then the function should return "3rd" + +test("should return '3rd' for 3", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); +}); + +// Case 4: Identify the ordinal number for 4 +// When the number is 4, +// Then the function should return "4th" + +test("should return '4th' for 4", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); +}); + +// Case 5: Identify the ordinal number for 11 +// When the number is 11, +// Then the function should return "11th" + +test("should return '11th' for 11", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); +}); + +// Case 6: Identify the ordinal number for 12 +// When the number is 12, +// Then the function should return "12th" + +test("should return '12th' for 12", () => { + expect(getOrdinalNumber(12)).toEqual("12th"); +}); + +// Case 7: Identify the ordinal number for 13 +// When the number is 13, +// Then the function should return "13th" + +test("should return '13th' for 13", () => { + expect(getOrdinalNumber(13)).toEqual("13th"); +}); + +// Case 8: Identify the ordinal number for 21 +// When the number is 21, +// Then the function should return "21st" + +test("should return '21st' for 21", () => { + expect(getOrdinalNumber(21)).toEqual("21st"); +}); + +// Case 9: Identify the ordinal number for 22 +// When the number is 22, +// Then the function should return "22nd" + +test("should return '22nd' for 22", () => { + expect(getOrdinalNumber(22)).toEqual("22nd"); +}); + +// Case 10: Identify the ordinal number for 23 +// When the number is 23, +// Then the function should return "23rd" + +test("should return '23rd' for 23", () => { + expect(getOrdinalNumber(23)).toEqual("23rd"); +}); + +// Case 11: Identify the ordinal number for 101 +// When the number is 101, +// Then the function should return "101st" + +test("should return '101st' for 101", () => { + expect(getOrdinalNumber(101)).toEqual("101st"); +}); + +// Case 12: Identify the ordinal number for 111 +// When the number is 111, +// Then the function should return "111th" + +test("should return '111th' for 111", () => { + expect(getOrdinalNumber(111)).toEqual("111th"); +}); + +// Case 13: Identify the ordinal number for 0 +// When the number is 0, +// Then the function should return "0th" + +test("should return '0th' for 0", () => { + expect(getOrdinalNumber(0)).toEqual("0th"); +}); + diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..cf4b2325d 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,20 @@ -function repeat() { - return "hellohellohello"; +function repeat(str, count) { + // Handle edge cases + if (count < 0) { + throw new Error("Count cannot be negative"); + } + + if (count === 0) { + return ""; + } + + // Build the repeated string + let result = ""; + for (let i = 0; i < count; i++) { + result += str; + } + + return result; } module.exports = repeat; diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 34097b09c..45cb41134 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -19,14 +19,31 @@ test("should repeat the string count times", () => { // case: handle Count of 1: // Given a target string str and a count equal to 1, // When the repeat function is called with these inputs, -// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition. +// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition +test("should return the original string for count of 1", () => { + const str = "hello"; + const count = 1; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("hello"); +}); // case: Handle Count of 0: // Given a target string str and a count equal to 0, // When the repeat function is called with these inputs, // Then it should return an empty string, ensuring that a count of 0 results in an empty output. +test("should return an empty string for count of 0", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual(""); +}); // case: Negative Count: // Given a target string str and a negative integer count, // When the repeat function is called with these inputs, // Then it should throw an error or return an appropriate error message, as negative counts are not valid. +test("should throw an error for negative count", () => { + const str = "hello"; + const count = -2; + expect(() => repeat(str, count)).toThrow("Count cannot be negative"); +}); \ No newline at end of file