diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..9640831dc 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 char = 0; + for(let i = 0;i < stringOfCharacters.length;i++){ + if(stringOfCharacters[i] === findCharacter){ + char++; + } + } + return char } 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..29ee53239 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 if the character doesn't appear in the string",()=>{ + const str ="aaaaa"; + const char = "s"; + 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..9aa720082 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,26 @@ function getOrdinalNumber(num) { - return "1st"; + const lastDigit=Number(num.toString().slice(-1)); + const lastTwoDigits=Number(num.toString().slice(-2)); + + if(typeof num !=="number"){ + return NaN; + } + + if(lastTwoDigits>=10 && lastTwoDigits<=13){ + return `${num}th`; + } + + if (lastDigit === 1) { + return `${num}st`; + } else if (lastDigit === 2) { + return `${num}nd`; + } else if (num === 3) { + return "3rd"; + } + + 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..d453c9c22 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,32 @@ const getOrdinalNumber = require("./get-ordinal-number"); test("should return '1st' for 1", () => { expect(getOrdinalNumber(1)).toEqual("1st"); }); + +test("should return '121st' for 121", () =>{ + expect(getOrdinalNumber(121)).toEqual("121st") +}) + +test("should return '2nd' for 2", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); +}); + + +test("should return '3rd' for 3", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); +}); + +test("should return '5th' for 5", () => { + expect(getOrdinalNumber(5)).toEqual("5th"); +}); + +test("should return '11th' for 11", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); +}); + +test("should return '-11th' for -11", () => { + expect(getOrdinalNumber(-11)).toEqual("-11th"); +}); + +test("should return NaN for s", () => { + expect(getOrdinalNumber("8")).toEqual(NaN); +}); \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..f8f1451e0 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,11 @@ -function repeat() { - return "hellohellohello"; +function repeat(str,count) { + if(count<0){ + return "Error: negative numbers are not valid"; + } + return (str.repeat(count)); } 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..f136cb49d 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -21,12 +21,33 @@ test("should repeat the string count times", () => { // 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. +test("should not repeat the string", ()=>{ +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", ()=>{ + 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 return error", ()=>{ + const str="hello"; + const count=-1; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("Error: negative numbers are not valid"); +})