diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..3b974e47e 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,11 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let charCount = 0; + for (let i = 0; i < stringOfCharacters.length; i++) { + if (stringOfCharacters[i] === findCharacter) { + charCount += 1; + } + } + return charCount; } 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..ec0e83437 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -4,7 +4,7 @@ const countChar = require("./count"); // When the countChar function is called with these inputs, // Then it should: -// Scenario: Multiple Occurrences +// Scenario: Multiple Occurrences // Given the input string str, // And a character char that may occur multiple times with overlaps within str (e.g., 'a' in 'aaaaa'), // When the function is called with these inputs, @@ -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 if the character does not occur",()=>{ + const str1="ads1" + const char1="l" + const count1=countChar(str1,char1) + expect(count1).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..4e6e2172d 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,18 @@ function getOrdinalNumber(num) { - return "1st"; + if (num < 1) { + throw new Error("Only integer numbers,bigger than 0"); + } + let lastDigit = num.toString().slice(-1); + let digitBeforeLast = num.toString().slice(-2, -1); + + if (lastDigit === "1" && digitBeforeLast !== "1") { + return num + "st"; + } else if (lastDigit === "2" && digitBeforeLast !== "1") { + return num + "nd"; + } else if (lastDigit === "3" && digitBeforeLast !== "1") { + return num + "rd"; + } + 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..8e2be45e7 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,89 @@ 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 11 +// When the number is 11, +// Then the function should return "11th" + +test("should return '11th' for 11", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); +}); + +// Case 5: 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 6: 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 7: Identify the ordinal number for 5 +// When the number is 5, +// Then the function should return "5th" +test("should return '5th' for 5", () => { + expect(getOrdinalNumber(5)).toEqual("5th"); +}); + +// Case 8: Identify the ordinal number for 1035 +// When the number is 1035, +// Then the function should return "1035th" +test("should return '1035th' for 1035", () => { + expect(getOrdinalNumber(1035)).toEqual("1035th"); +}); + +// Case 9: 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 10: Identify the ordinal number for 321 +// When the number is 321, +// Then the function should return "321st" +test("should return '321st' for 321", () => { + expect(getOrdinalNumber(321)).toEqual("321st"); +}); + +// Case 11: Identify the ordinal number for 0 +// When the number is 0, +// Then the function should throw error +test("should return error for 0", () => { + expect(() => getOrdinalNumber(0)).toThrow( + "Only integer numbers,bigger than 0" + ); +}); + +// Case 12: Identify the ordinal number for -1 +// When the number is -1, +// Then the function should throw error +test("should return error for -1", () => { + expect(() => getOrdinalNumber(-1)).toThrow( + "Only integer numbers,bigger than 0" + ); +}); \ 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..398a3db59 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,5 +1,8 @@ -function repeatStr() { - return "hellohellohello"; +function repeatStr(str, count) { + if (count < 0) { + throw new Error("Only positive integers accepted"); + } + 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 fc59d019e..2a7c93560 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -20,13 +20,32 @@ 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, ensuring that a count of 1 results in no repetition. +test("should repeat the string count times", () => { + 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, ensuring that a count of 0 results in an empty output. +test("should repeat the string count times", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual(""); +}); // case: 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 or return an appropriate error message, as negative counts are not valid. +test("should repeat the string count times", () => { + const str = "hello"; + const count = -1; + expect(() => repeatStr(str, count)).toThrow( + "Only positive integers accepted" + ); +});