diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..17e1f035b7 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,5 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + return stringOfCharacters.split("").filter(char => char === findCharacter).length; } -module.exports = countChar; +module.exports = countChar; \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf7..f8256d93ed 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -1,4 +1,5 @@ // implement a function countChar that counts the number of times a character occurs in a string +// implement a function countChar that counts the number of times a character occurs in a string const countChar = require("./count"); // Given a string `str` and a single character `char` to search for, // When the countChar function is called with these inputs, @@ -9,7 +10,6 @@ const countChar = require("./count"); // And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'), // When the function is called with these inputs, // Then it should correctly count occurrences of `char`. - test("should count multiple occurrences of a character", () => { const str = "aaaaa"; const char = "a"; @@ -22,3 +22,9 @@ 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", () => { + const str = "aaaaa"; + const char = "b"; + 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 f95d71db13..84477bbe98 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,20 @@ function getOrdinalNumber(num) { - return "1st"; + const lastTwoDigits = num % 100; + const lastDigit = num % 10; + + if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { + return `${num}th`; + } + + if (lastDigit === 1) { + return `${num}st`; + } else if (lastDigit === 2) { + return `${num}nd`; + } else if (lastDigit === 3) { + return `${num}rd`; + } else { + return `${num}th`; + } } -module.exports = getOrdinalNumber; +module.exports = getOrdinalNumber; \ No newline at end of file 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..08168834b2 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -9,12 +9,29 @@ const getOrdinalNumber = require("./get-ordinal-number"); // 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. +// Case 4: Numbers ending with 11, 12, or 13 (the exceptions) +// These should always get "th", even though they end in 1, 2, or 3. + // 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", () => { - expect(getOrdinalNumber(1)).toEqual("1st"); - expect(getOrdinalNumber(21)).toEqual("21st"); - expect(getOrdinalNumber(131)).toEqual("131st"); +// Case 4: Numbers ending with 11, 12, or 13 (the exceptions) +// These should always get "th", even though they end in 1, 2, or 3. +test("should append 'th' for numbers ending with 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"); }); + +// Case 5: Numbers ending with 4-9, and 0 +// These should always get "th". +test("should append 'th' for numbers ending with 0 or 4-9", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(15)).toEqual("15th"); + expect(getOrdinalNumber(20)).toEqual("20th"); + expect(getOrdinalNumber(100)).toEqual("100th"); +}); \ No newline at end of file