diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..f973f02be 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,14 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + + for (let char of stringOfCharacters) { + if (char === 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 42baf4b4b..2de7c7b10 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,10 @@ 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 occur in the string", () => { + const str = "hello"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db1..46528f0b6 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,17 @@ function getOrdinalNumber(num) { - return "1st"; + const lastDigit = num % 10; + const lastTwoDigits = num % 100; + + if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { + return `${num}th`; + } + + + if (lastDigit === 1) return `${num}st`; + if (lastDigit === 2) return `${num}nd`; + if (lastDigit === 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 dfe4b6091..ffc75860d 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -1,13 +1,55 @@ const getOrdinalNumber = require("./get-ordinal-number"); -// In this week's prep, we started implementing getOrdinalNumber -// continue testing and implementing getOrdinalNumber for additional cases -// Write your tests using Jest - remember to run your tests often for continual feedback +// Category 1: Numbers ending in 11, 12, 13 → always "th" +test("appends 'th' to numbers ending in 11, 12, or 13", () => { + const numbers = [11, 12, 13, 111, 1012, 1313]; + numbers.forEach(num => { + expect(getOrdinalNumber(num)).toEqual(`${num}th`); + }); +}); + +// Category 2: Numbers ending in 1 → "st" (except those ending in 11) +test("appends 'st' to numbers ending in 1 except those ending in 11", () => { + const numbers = [1, 21, 101, 1001]; + numbers.forEach(num => { + expect(getOrdinalNumber(num)).toEqual(`${num}st`); + }); +}); -// Case 1: Identify the ordinal number for 1 -// When the number is 1, -// Then the function should return "1st" +// Category 3: Numbers ending in 2 → "nd" (except those ending in 12) +test("appends 'nd' to numbers ending in 2 except those ending in 12", () => { + const numbers = [2, 22, 102, 202]; + numbers.forEach(num => { + expect(getOrdinalNumber(num)).toEqual(`${num}nd`); + }); +}); + +// Category 4: Numbers ending in 3 → "rd" (except those ending in 13) +test("appends 'rd' to numbers ending in 3 except those ending in 13", () => { + const numbers = [3, 23, 103, 1003]; + numbers.forEach(num => { + expect(getOrdinalNumber(num)).toEqual(`${num}rd`); + }); +}); + +// Category 5: All other numbers → "th" +test("appends 'th' to numbers that don't meet special suffix rules", () => { + const numbers = [4, 5, 6, 9, 20, 100, 1004, 1009]; + numbers.forEach(num => { + expect(getOrdinalNumber(num)).toEqual(`${num}th`); + }); +}); + +// Error handling - Non-integers +test("throws an error when number is not an integer", () => { + const invalidInputs = [1.5, "3", null, undefined, {}, [], NaN]; + + invalidInputs.forEach(input => { + expect(() => getOrdinalNumber(input)).toThrow("Input must be an integer"); + }); +}); -test("should return '1st' for 1", () => { - expect(getOrdinalNumber(1)).toEqual("1st"); +// Error handling - Negative values +test("throws an error when number is negative", () => { + expect(() => getOrdinalNumber(-1)).toThrow("Input must be a non-negative integer"); }); diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 3838c7b00..9dee7447e 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,5 +1,8 @@ -function repeatStr() { - return "hellohellohello"; +function repeatStr(str, count) { + if (count < 0) { + throw new Error("Count cannot be negative"); + } + 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 fc59d019e..e53525dba 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -1,32 +1,31 @@ -// 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: repeat String: -// Given a target string str and a positive integer count, -// When the repeatStr function is called with these inputs, -// Then it should repeat the str count times and return a new string containing the repeated str values. - -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, ensuring that a count of 1 results in no repetition. +test("should return 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, ensuring that a count of 0 results in an empty output. +test("should return empty string when count is 0", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual(""); +}); // case: Negative Count: // Given a target string str and a negative integer count, // When the repeatStr 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 = "hello"; + const count = -3; + expect(() => repeatStr(str, count)).toThrow(); +});