diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..f8d2898f7 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 output = 0; + for( let i=0; i <= stringOfCharacters.length; i++ ) { + if(stringOfCharacters[i] === findCharacter) + output++; + } + return output; } 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..ce08b2345 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 there are no occurrences at all", () => { + const str = "Hello World!"; + const char = "a"; + 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..5af6dceb9 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,27 @@ function getOrdinalNumber(num) { - return "1st"; + if (!Number.isInteger(num) || num <= 0) { + throw new Error("Input must be a positive integer."); + } + + const lastTwoDigits = num % 100; + const lastDigit = num % 10; + + 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`; + } + return num; } +//console.log(getOrdinalNumber(21)); // "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 dfe4b6091..9f46f37a2 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -8,6 +8,52 @@ const getOrdinalNumber = require("./get-ordinal-number"); // When the number is 1, // Then the function should return "1st" -test("should return '1st' for 1", () => { +test("Numbers that end with `st`", () => { expect(getOrdinalNumber(1)).toEqual("1st"); + expect(getOrdinalNumber(21)).toEqual("21st"); + expect(getOrdinalNumber(101)).toEqual("101st"); + expect(getOrdinalNumber(28491)).toEqual("28491st"); }); + +// Case 2: "Numbers that end with `nd` +test("Numbers that end with `nd`", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(32)).toEqual("32nd"); + expect(getOrdinalNumber(202)).toEqual("202nd"); + expect(getOrdinalNumber(4502)).toEqual("4502nd"); +}); + +// Case 3:: "Numbers that end with `rd` +test("Numbers that end with `rd`", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(43)).toEqual("43rd"); + expect(getOrdinalNumber(123)).toEqual("123rd"); + expect(getOrdinalNumber(6703)).toEqual("6703rd"); +}); + +// Case 4: "Numbers that end with `th` +test("Numbers that end with `th`", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(25)).toEqual("25th"); + expect(getOrdinalNumber(1124)).toEqual("1124th"); + expect(getOrdinalNumber(9999)).toEqual("9999th"); +}); + +// Case 5: "Numbers that are exceptions (11, 12, 13) +test("Numbers that are exceptions (11, 12, 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"); +}); + +// Case 6: "Non-positive integers (NaN, Infinity, non-integers) +test("should throw an error stating that input must be a positive integer", () => { + expect(() => getOrdinalNumber(NaN)).toThrow("Input must be a positive integer."); + expect(() => getOrdinalNumber(Infinity)).toThrow("Input must be a positive integer."); + expect(() => getOrdinalNumber("hello")).toThrow("Input must be a positive integer."); + expect(() => getOrdinalNumber([0, 1, 2])).toThrow("Input must be a positive integer."); +}); \ 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..40d7455b0 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,8 @@ -function repeat() { - return "hellohellohello"; +function repeat(str, count) { + if (!Number.isInteger(count) || count < 0) { + throw new Error("Count must be a valid number"); + } + 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..6045f245b 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -20,13 +20,39 @@ test("should repeat the string count times", () => { // Given a target string str and a count equal to 1, // 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 repeat the string only 1 time (no repetition)", () => { + 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 repeat the string 0 times ( means the output will be 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 throw an error if count is not a valid number", () => { + const str = "hello"; + const count = -8; + expect(() => repeat(str, count)).toThrow("Count must be a valid number"); +}); + +test("should throw an error if count is not a valid number", () => { + const str = "hello"; + const count = "abc"; + expect(() => repeat(str, count)).toThrow("Count must be a valid number"); +}); + + +