diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..7468f4e1f4 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 +// } function countChar(stringOfCharacters, findCharacter) { - return 5 + let countChar = 0; + for (let stringNum = 0; stringNum < stringOfCharacters.length; stringNum++) { + if (findCharacter === stringOfCharacters[stringNum]){ + countChar++; + } + } + return countChar; } - module.exports = countChar; diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf7..75f4cc5054 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -17,8 +17,29 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(5); }); +test("should count multiple occurrences of a character with spaces", () => { + const str = "eee ee e e"; + const char = "e"; + const count = countChar(str, char); + expect(count).toEqual7; +}); + +test("should count multiple occurrences of a character with random characters", () => { + const str = "ajhyabhaa kaak"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(6); +}) + // Scenario: No Occurrences // Given the input string `str`, // 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 return 0 with no occurrence of the character", () => { + const str ="abcd"; + const char = "e"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db13..2de7f70695 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,22 @@ +// function getOrdinalNumber(num) { +// return "1st"; +// } + function getOrdinalNumber(num) { - return "1st"; + let text = num.toString(); + + if (Math.abs(num) >= 11 && Math.abs(num) <=13) { + return (text + "th") + } else if (text.slice(-1) === "1") { + return (text + "st") + } else if (text.slice(-1) === "2") { + return (text + "nd") + } else if (text.slice(-1) === "3") { + return (text +"rd") + }; + + return (text + "th"); } +// getOrdinalNumber(1)).toEqual("1st") 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 adfa58560f..8e8b8a4e6b 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,32 @@ 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: Numbers ending with 2 should return with "nd" to the number +// Unless number 12 should return with the "th" to the number +test("should append 'nd' for numbers ending in 2", () => { + expect(getOrdinalNumber(22)).toEqual("22nd"); +}); + +// Case 3: Numbers ending with 3 should return with a "rd" to the number +// Unless number is 13 then it should return with the "th" to the number +test("should append 'rd' for numbers ending with 3", () => { + expect(getOrdinalNumber(23)).toEqual("23rd"); +}); + + + +// Case 4: all other numbers should return with a "th" to the number + +test("should append 'th' for all other numbers", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(194)).toEqual("194th"); +}); + +// Case 5: Negative numbers follow same rules. +test("should follow rules as previous tests while still showing '-' value", () => { + expect(getOrdinalNumber(-12)).toEqual("-12th"); + expect(getOrdinalNumber(-52)).toEqual("-52nd"); +}); \ 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 2af0a2cea7..8133246fd8 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,7 +1,17 @@ -function repeatStr() { +function repeatStr(str, count) { + let result = ""; + if (count < 0) { + throw new Error("cannot be negative number") + } + + for (let i=0; i < count; i++) { + result = result + str + } // Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat). // The goal is to re-implement that function, not to use it. - return "hellohellohello"; -} + return result; + +}; 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 a3fc1196c4..d6b838628e 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -20,13 +20,33 @@ 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 return the string as the str for counting 1 time", () => { + 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. +test("should return empty string when count is 0", ()=> { + const str = "hello"; + const count = 0; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).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 error when count is negative", ()=> { + const str="hello"; + const count =-6; + expect(() => repeatStr(str, count)).toThrow("cannot be negative number"); + +});