diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..ff2db9a54 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,8 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + for (const c of stringOfCharacters) { + if (c === findCharacter) count++; + } + return count; } - module.exports = countChar; diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf..9f6ab14e7 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -10,11 +10,41 @@ const countChar = require("./count"); // When the function is called with these inputs, // Then it should correctly count occurrences of `char`. -test("should count multiple occurrences of a character", () => { +/*test("should count multiple occurrences of a character", () => { const str = "aaaaa"; const char = "a"; const count = countChar(str, char); expect(count).toEqual(5); +});*/ + +describe("countChar", () => { + test("counts repeated characters in a simple string", () => { + expect(countChar("aaaaa", "a")).toEqual(5); + }); + + test("counts characters in a mixed string", () => { + expect(countChar("absankama", "a")).toEqual(4); + }); + + test("counts characters including spaces", () => { + expect(countChar("ab san kama", "a")).toEqual(4); + }); + + test("counts characters in string with symbols", () => { + expect(countChar("a-+&£$%?/|b san kama", "a")).toEqual(4); + }); + + test("returns 0 when character is not found", () => { + expect(countChar("-+&£$%?/|b sn km", "a")).toEqual(0); + }); + + test("returns 0 for empty string", () => { + expect(countChar("", "a")).toEqual(0); + }); + + test("is case sensitive", () => { + expect(countChar("AaAa", "a")).toEqual(2); + }); }); // Scenario: No Occurrences diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db1..34226272f 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,11 @@ function getOrdinalNumber(num) { - return "1st"; + const endDigit = num % 10; + const endTwoDigits = num % 100; + if (endTwoDigits >= 11 && endTwoDigits <= 13) return num + "th"; + if (endDigit === 1) return num + "st"; + if (endDigit === 2) return num + "nd"; + if (endDigit === 3) 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 adfa58560..ccc6198ac 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -13,8 +13,36 @@ const getOrdinalNumber = require("./get-ordinal-number"); // Case 1: Numbers ending with 1 (but not 11) // When the number ends with 1, except those ending with 11, // Then the function should return a string by appending "st" to the number. -test("should append 'st' for numbers ending with 1, except those ending with 11", () => { +/*test("should append 'st' for numbers ending with 1, except those ending with 11", () => { expect(getOrdinalNumber(1)).toEqual("1st"); expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); +});*/ + +describe("getOrdinalNumber", () => { + test("appends th for numbers ending in 11, 12, or 13", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(111)).toEqual("111th"); + expect(getOrdinalNumber(112)).toEqual("112th"); + expect(getOrdinalNumber(113)).toEqual("113th"); + }); + test("appends st for numbers ending in 1 but not 11", () => { + expect(getOrdinalNumber(1)).toEqual("1st"); + expect(getOrdinalNumber(131)).toEqual("131st"); + }); + test("appends nd for numbers ending in 2 but not 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(932)).toEqual("932nd"); + }); + test("appends rd for numbers ending in 3 but not 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(33)).toEqual("33rd"); + }); + test("appends th for all other numbers", () => { + expect(getOrdinalNumber(20)).toEqual("20th"); + expect(getOrdinalNumber(24)).toEqual("24th"); + expect(getOrdinalNumber(100)).toEqual("100th"); + }); }); diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 2af0a2cea..b4dfe0e51 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,7 +1,13 @@ -function repeatStr() { +function repeatStr(str, count) { // 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"; + let combinedStr = ""; + if (count < 0) throw new Error("Invalid Count"); + + for (let i = 0; i < count; i++) { + combinedStr += str; + } + return combinedStr; } 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..84dab0bb3 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -9,11 +9,26 @@ const repeatStr = require("./repeat-str"); // When the repeatStr function is called with these inputs, // Then it should return a string that contains the original `str` repeated `count` times. -test("should repeat the string count times", () => { - const str = "hello"; - const count = 3; - const repeatedStr = repeatStr(str, count); - expect(repeatedStr).toEqual("hellohellohello"); +describe("repeatStr", () => { + test("repeats string count times when count is greater than 1", () => { + expect(repeatStr("hello", 3)).toEqual("hellohellohello"); + }); + + test("returns original string when count is 1", () => { + expect(repeatStr("hello", 1)).toEqual("hello"); + }); + + test("returns empty string when count is 0", () => { + expect(repeatStr("hello", 0)).toEqual(""); + }); + + test("throws an error when count is negative", () => { + expect(() => repeatStr("hello", -3)).toThrow("Invalid Count"); + }); + + test("returns empty string when the input string is empty", () => { + expect(repeatStr("", 3)).toEqual(""); + }); }); // Case: handle count of 1: