diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..d739edfec3 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,9 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + if (!stringOfCharacters.includes(findCharacter)) { + return 0; + } + return stringOfCharacters.split("").filter((char) => char === findCharacter) + .length; } 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..df61f3207c 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -17,8 +17,21 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(5); }); +test("should count multiple occurrences of a different character", () => { + const str = "banana"; + const char = "n"; + const count = countChar(str, char); + expect(count).toEqual(2); +}); + // Scenario: No Occurrences // Given the input string `str`, // 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 no occurrences of a character are found", () => { + const str = "aaaaa"; + const char = "b"; + 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 f95d71db13..8c3379d044 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"; + // check if the input is not a number, not an integer, or if it's negative + if (typeof num !== "number" || !Number.isInteger(num) || num < 0) { + throw new Error("Invalid input: number must be a non-negative integer"); + } + +if (num % 100 >= 11 && num % 100 <= 13) { // check for numbers ending with 11, 12, or 13 + return num + "th"; + } else if (num % 10 === 1) { // check for numbers ending with 1 (but not 11) + return num + "st"; + } else if (num % 10 === 2) { // check for numbers ending with 2 (but not 12) + return num + "nd"; + } else if (num % 10 === 3) { // check for numbers ending with 3 (but not 13) + return num + "rd"; + } + // For all other numbers, append "th" + 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..0675d8c570 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -18,3 +18,68 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); }); + +// Case 2: Numbers ending with 2 (but not 12) +// When the number ends with 2, except those ending with 12, +// Then the function should return a string by appending "nd" to the number. +test("should append 'nd' for numbers ending with 2, except those ending with 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(132)).toEqual("132nd"); +}); + +// Case 3: Numbers ending with 3 (but not 13) +// When the number ends with 3, except those ending with 13, +// Then the function should return a string by appending "rd" to the number. +test("should append 'rd' for numbers ending with 3, except those ending with 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(133)).toEqual("133rd"); +}); + +// Case 4: Numbers ending with 11, 12, or 13 +// When the number ends with 11, 12, or 13, +// Then the function should return a string by appending "th" to the number. +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"); +}); + +// Case 5: All other numbers +// When the number does not fall into any of the above categories, +// Then the function should return a string by appending "th" to the number. +test("should append 'th' for all other numbers", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(5)).toEqual("5th"); + expect(getOrdinalNumber(6)).toEqual("6th"); + expect(getOrdinalNumber(7)).toEqual("7th"); + expect(getOrdinalNumber(8)).toEqual("8th"); + expect(getOrdinalNumber(9)).toEqual("9th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(127)).toEqual("127th"); +}); + +// Case 6: Negative numbers +// When the number is negative, +// Then the function should throw an error, as negative numbers are not valid inputs. +test("should throw an error for negative numbers", () => { + expect(() => getOrdinalNumber(-1)).toThrow("Invalid input: number must be a non-negative integer"); + expect(() => getOrdinalNumber(-22)).toThrow("Invalid input: number must be a non-negative integer"); +}); + +// Case 7: Non-integer numbers +// When the number is not an integer (e.g., a float), +// Then the function should throw an error, as only non-negative integers are valid inputs. +test("should throw an error for non-integer numbers", () => { + expect(() => getOrdinalNumber(1.5)).toThrow("Invalid input: number must be a non-negative integer"); + expect(() => getOrdinalNumber(2.7)).toThrow("Invalid input: number must be a non-negative integer"); +}); + +// Case 8: Non-number inputs +// When the input is not a number (e.g., a string, object, or array), +// Then the function should throw an error, as only non-negative integers are valid inputs. +test("should throw an error for non-number inputs", () => { + expect(() => getOrdinalNumber("1")).toThrow("Invalid input: number must be a non-negative integer"); + expect(() => getOrdinalNumber("abcd")).toThrow("Invalid input: number must be a non-negative integer"); +}); \ 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 2af0a2cea7..c380561b7a 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,7 +1,11 @@ -function repeatStr() { +function repeatStr(stringOfCharacters, count) { // Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat). // The goal is to re-implement that function, not to use it. - return "hellohellohello"; + if (count === 0) { + return ""; + } else if (count > 0) { + return stringOfCharacters.repeat(count); + } + throw Error("Invalid input: count must be a non-negative integer"); } - 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..19a4e03bc9 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -21,12 +21,32 @@ test("should repeat the string count times", () => { // 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 = "world"; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("world"); +}); + // 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 = "test"; + const count = 0; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual(""); +}); + // Case: Handle 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, as negative counts are not valid. + +test("should throw an error when count is negative", () => { + const str = "error"; + const count = -2; + expect(() => repeatStr(str, count)).toThrow("Invalid input: count must be a non-negative integer"); +}); \ No newline at end of file