diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..7b4d2ef51 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,10 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 -} + let count = 0 + for(let i=0;i { + expect(countChar("tired", "d")).toEqual(1) +}) + // Scenario: Multiple Occurrences // Given the input string `str`, @@ -22,3 +26,6 @@ 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 count no occurrences of a character", () => { + expect(countChar("bananataste","w")).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..7a2c1f700 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,20 @@ function getOrdinalNumber(num) { - return "1st"; + let lastDigit = num % 10; + let lastTwoDigits = num % 100; + if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { + return num + "th"; + } + + switch (lastDigit) { + case 1: + return num + "st"; + case 2: + return num + "nd"; + case 3: + return num + "rd"; + default: + 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 adfa58560..c7e8954ea 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -18,3 +18,31 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); }); + +// case 2: last two digits ending with 11,12,13 +test("should append 'th' for numbers with last digits 11,12,13", () =>{ + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(313)).toEqual("313th") + expect(getOrdinalNumber(2112)).toEqual("2112th") +}) + +// Case 3: Numbers ending with 2(but not 12) +test("should append 'nd' for numbers ending with 2, except those ending with 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(52)).toEqual("52nd"); + expect(getOrdinalNumber(232)).toEqual("232nd"); +}); + +// Case 4: Numbers ending with 3(but not 13) +test("should append 'rd' for numbers ending with 3, except those ending with 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(83)).toEqual("83rd"); + expect(getOrdinalNumber(463)).toEqual("463rd"); +}); + +// case 5: Numbers ending with 4-9 and 0 +test("should append 'th' for numbers with last 4-9 and 0", () =>{ + expect(getOrdinalNumber(8)).toEqual("8th"); + expect(getOrdinalNumber(3130)).toEqual("3130th") + expect(getOrdinalNumber(2119)).toEqual("2119th") +}) \ 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..193f1f8bd 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,5 +1,9 @@ -function repeatStr() { - return "hellohellohello"; +function repeatStr(str, count) { + if (count < 0) { + throw new Error("Count cannot be negative"); + } + + 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..ad1c378d2 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -20,13 +20,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 repeat the string only once", () => { + expect(repeatStr("nihao",1)).toEqual("nihao"); +}); // 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 repeat the string 0", () => { + expect(repeatStr("newyear",0)).toEqual(""); +}); // 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 when count is negative", () => { + expect(() => repeatStr("celebration", -5)) + .toThrow("Count cannot be negative"); +}); \ No newline at end of file