diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..d59e425d6 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,11 @@ 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 42baf4b4b..f05c1baa8 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 exist in string", () => { + const str = "hello world"; + const char = "a"; + 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..037c45bd4 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 n = Math.abs(parseInt(num, 10)); + const lastTwoDigits = n % 100; + if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { + return `${n}th`; + } + const lastDigit = n % 10; + switch (lastDigit) { + case 1: + return `${n}st`; + case 2: + return `${n}nd`; + case 3: + return `${n}rd`; + default: + return `${n}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..181dfe7a8 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -4,10 +4,35 @@ const getOrdinalNumber = require("./get-ordinal-number"); // continue testing and implementing getOrdinalNumber for additional cases // Write your tests using Jest - remember to run your tests often for continual feedback -// Case 1: Identify the ordinal number for 1 -// When the number is 1, -// Then the function should return "1st" +// Case 1: Numbers ending in 1 except 11 -> "st" +test("should return correct suffix for numbers ending in 1 except 11", () => { + const stNumbers = [1, 21, 101, 121]; + for (const num of stNumbers) { + expect(getOrdinalNumber(num)).toEqual(`${num}st`); + } +}); + +// Case 2: Numbers ending in 2 except 12 -> "nd" +test("should return correct suffix for numbers ending in 2 except 12", () => { + const ndNumbers = [2, 22, 102, 122]; + for (const num of ndNumbers) { + expect(getOrdinalNumber(num)).toEqual(`${num}nd`); + } +}); -test("should return '1st' for 1", () => { - expect(getOrdinalNumber(1)).toEqual("1st"); +// Case 3: Numbers ending in 3 except 13 -> "rd" +test("should return correct suffix for numbers ending in 3 except 13", () => { + const rdNumbers = [3, 23, 103, 123]; + for (const num of rdNumbers) { + expect(getOrdinalNumber(num)).toEqual(`${num}rd`); + } }); + +// Case 4: Numbers ending in 4-9, 0, and 11-13 -> "th" +test("should return correct suffix for numbers ending in 4-9, 0, and 11-13", () => { + const thNumbers = [4, 5, 6, 7, 8, 9, 11, 12, 13, 100]; + for (const num of thNumbers) { + expect(getOrdinalNumber(num)).toEqual(`${num}th`); + } +}); + diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 3838c7b00..8188ac626 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 repeat(str, count) { + if (!Number.isInteger(count) || count < 0) { + throw Error("Invalid count value"); + } + return str.repeat(count); } -module.exports = repeatStr; +module.exports = repeat; diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index fc59d019e..b88d12d3d 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -4,7 +4,7 @@ const repeatStr = require("./repeat-str"); // When the repeatStr function is called with these inputs, // Then it should: -// case: repeat String: +// case 1: 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. @@ -12,21 +12,45 @@ const repeatStr = require("./repeat-str"); test("should repeat the string count times", () => { const str = "hello"; const count = 3; - const repeatedStr = repeatStr(str, count); - expect(repeatedStr).toEqual("hellohellohello"); + expect(repeatStr(str, count)).toEqual("hellohellohello"); }); -// case: handle Count of 1: +// case 2: 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 the original string when count is 1", () => { + const str = "hello"; + const count = 1; + expect(repeatStr(str, count)).toEqual("hello"); +}); -// case: Handle Count of 0: +// case 3: 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 an empty string when count is 0", () => { + const str = "hello"; + const count = 0; + expect(repeatStr(str, count)).toEqual(""); +}); -// case: Negative Count: +// case 4: 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 = -2; + expect(() => repeatStr(str, count)).toThrow("Invalid count value"); +}); + +// case 5: Non-integer Count: +// Given a target string str and a non-integer count (e.g., a float or a string), +// When the repeatStr function is called with these inputs, +// Then it should throw an error or return an appropriate error message, as non-integer counts are not valid. +test("should throw an error for non-integer count", () => { + const str = "hello"; + const count = 2.5; + expect(() => repeatStr(str, count)).toThrow("Invalid count value"); +});