diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..79977793e5 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 < stringOfCharacters.length; i++) { + if (stringOfCharacters[i] === 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 179ea0ddf7..0b0f9845df 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,21 @@ 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 return 0 when the character does not occur in the string", () => { + const str = "hello world"; + const char = "x"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// Scenario: Case Sensitivity +// Given the input string `str`, +// And a character `char` that occurs in `str` but with different cases (e.g., 'a' and 'A'), +// When the function is called with these inputs, +// Then it should count only the occurrences of `char` that match the case provided in the input. +test("should count only occurrences of the character that match the case", () => { + const str = "AaAaA"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(2); +}); diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db13..d3fd07c153 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"; + const lastDigit = num % 10; + const 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`; + } } 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..98d6bb7a13 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -8,13 +8,36 @@ const getOrdinalNumber = require("./get-ordinal-number"); // Listing individual values, however, can quickly lead to an unmanageable number of test cases. // Instead of writing tests for individual numbers, consider grouping all possible input values // into meaningful categories. Then, select representative samples from each category to test. -// This approach improves coverage and makes our tests easier to maintain. +// This approach improves coverage and makes our tests easier to maintain -// 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. +// Case 1: numbers ending with 1 (but not 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"); }); + +// Case 2: numbers ending with 2 (except 12) +test("should append 'nd' for numbers ending with 2 except those ending with 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); +}); + +// Case 3: numbers ending with 3 (except 13) +test("should append 'rd' for numbers ending with 3 except those ending with 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); +}); + +// Case 4: numbers ending with th +test("should append 'th' for numbers ending with 4-9 or 0", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(10)).toEqual("10th"); +}); + +// Case 5: special cases 11, 12, 13 +test("should append 'th' for numbers ending with 11, 12, 13", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); +}); \ 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 3838c7b003..7850fb691a 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,5 +1,5 @@ -function repeatStr() { - return "hellohellohello"; +function repeatStr(str, count) { + return str.repeat(count); } 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..84bdd2e803 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -1,5 +1,46 @@ // Implement a function repeatStr const repeatStr = require("./repeat-str"); + +// Given a target string `str` and a positive integer `count`, +// When the repeatStr function is called with these inputs, +// Then it should: + +// Case: handle multiple repetitions: +// Given a target string `str` and a positive integer `count` greater than 1, +// 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"); +}); + +// Case: handle count of 1: +// 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 original string when count is 1", () => { + 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 an empty string when count is 0", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeatStr(str, count); + + expect(repeatedStr).toEqual(""); +}); // Implement a function repeatStr + // Given a target string `str` and a positive integer `count`, // When the repeatStr function is called with these inputs, // Then it should: