diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..2e9b762d7 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,13 @@ 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..a25d6b24b 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,25 @@ function getOrdinalNumber(num) { - return "1st"; + lastDigit = num % 10; + 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"; + } } +console.log(getOrdinalNumber(112)) + + + 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..fbcb3eb55 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) return str.repeat(count) + if (count===0) return "" + if (count<0) return"invalid count" } -module.exports = repeatStr; +console.log(repeatStr("hello",5)) +console.log(repeatStr("nihao",1)) +console.log(repeatStr("nihao",-5)) + +module.exports = repeatStr; \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index a3fc1196c..58087b0c6 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -20,13 +20,22 @@ 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 repeat the string negative times", () => { + expect(repeatStr("celebration", -5)).toEqual("invalid count"); +}); \ No newline at end of file