diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..961d707dd 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,12 @@ -function countChar(stringOfCharacters, findCharacter) { - return 5 +function countChar(str, char) { + if (!char || char.length !== 1) return 0; // ensure char is a single character + let count = 0; + for (let i = 0; i < str.length; i++) { + if (str[i] === char) { + 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 42baf4b4b..014aba203 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,44 @@ 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 when character does not exist in the string", () => { + const str = "mnopqrst"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// we can add more test cases to make sure our function is working as expected +// Scenario: Case Sensitivity +// Given the input string str, +// And a character char that exists in str but with different casing (e.g., 'A' in 'aAaAa'), +// When the function is called with these inputs, +// Then it should treat char as case-sensitive and only count occurrences that match the exact casing (e.g., 'A' appears two times in 'aAaAa'). + +test("should treat character as case-sensitive", () => { + const str = "aAaAa"; + const char = "A"; + const count = countChar(str, char); + expect(count).toEqual(2); +}); + +// we can also add empty string test case +// Scenario: Empty String +// Given an empty input string str, +// And any character char, +// When the function is called with these inputs, +// Then it should return 0, indicating that no occurrences of char can be found in an empty str. + +test("should return 0 for empty string", () => { + const str = ""; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// We can run this test file using the command `npx jest count.test.js` +// in the terminal. Making sure we are in the directory where this file is located. +// If we have Jest installed globally, you can simply run `jest count.test.js` +// instead. If you have added a test script to your package.json file, you can also run +// `npm test count.test.js` to execute the tests. \ 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..0938cd9fe 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,21 @@ function getOrdinalNumber(num) { - return "1st"; + if (!Number.isInteger(num) || num <= 0) { + return "Invalid input"; + } + if (num % 100 >= 11 && num % 100 <= 13) { + return num + "th"; + } + switch (num % 10) { + 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 dfe4b6091..c22dcb0b4 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", () => { +// category 1: ends with 1 (but not 11) -> "st" +test("append 'st' to numbers ending in 1, except those ending in 11", () => { expect(getOrdinalNumber(1)).toEqual("1st"); + expect(getOrdinalNumber(21)).toEqual("21st"); + expect(getOrdinalNumber(101)).toEqual("101st"); }); + +// Case 2: Identify the ordinal number for 2 +// When the number is 2, +// Then the function should return "2nd" + +// category 2: ends with 2 (but not 12) -> "nd" +test("append 'nd' to numbers ending in 2, except those ending in 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(102)).toEqual("102nd"); +}); + +// Case 3: Identify the ordinal number for 3 +// When the number is 3, +// Then the function should return "3rd" + +// category 3: ends with 3 (but not 13) -> "rd" +test("append 'rd' to numbers ending in 3, except those ending in 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(103)).toEqual("103rd"); +}); + +// Case 4: Identify the ordinal number for 4 +// When the number is 4, +// Then the function should return "4th" + +// category 4: all other numbers -> "th" +test("append 'th' to numbers ending in 0, 4–9, or 11–13", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(14)).toEqual("14th"); + expect(getOrdinalNumber(111)).toEqual("111th"); + expect(getOrdinalNumber(112)).toEqual("112th"); + expect(getOrdinalNumber(113)).toEqual("113th"); + expect(getOrdinalNumber(114)).toEqual("114th"); +}); + + + diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..6d908e0d7 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,30 @@ -function repeat() { - return "hellohellohello"; +function repeat(str, count) { + if (arguments.length !== 2) { + throw new Error("Function requires exactly two arguments: str and count."); + } + + if (typeof str !== "string") { + throw new Error("First argument must be a string."); + } + + if (!Number.isSafeInteger(count) || count < 0) { + throw new Error("Second argument must be a non-negative integer."); + } + + if (count === 0) { + return ""; + } + + if (count === 1) { + return str; + } + + let result = ""; + for (let i = 0; i < count; i++) { + result += str; + } + return result; } + 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..7d61098c1 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -21,12 +21,35 @@ test("should repeat the string count times", () => { // 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 return the original string when count is 1", () => { + const str = "world"; + const count = 1; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("world"); +}); + // 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 return an empty string when count is 0", () => { + const str = "test"; + 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 for negative count", () => { + const str = "error"; + const count = -2; + expect(() => repeat(str, count)).toThrow( + "Second argument must be a non-negative integer." + ); +}); +