diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..5351dd6cb 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 variable = 0; // moteghayer baraye shomaresh + + for (let i = 0; i < stringOfCharacters.length; i++) { + if (stringOfCharacters[i] === findCharacter) { + variable++; + } + } + + return variable; } 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..159ebb371 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -1,5 +1,6 @@ // 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, // Then it should: @@ -22,3 +23,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 no occurrences are found", () => { + 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..e44166b23 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"; + // Handle the 'teen' exceptions (11, 12, 13) that always end in 'th' + if (num % 100 >= 11 && num % 100 <= 13) { + return num + "th"; + } + + // Identify the last digit to determine the correct suffix for all other numbers + const lastDigit = num % 10; + + 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..057b264b7 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,38 @@ 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 +// Given a number, +// When the getOrdinalNumber function is called with this input, +// Then it should return the correct ordinal string. // Case 1: Identify the ordinal number for 1 -// When the number is 1, -// Then the function should return "1st" - test("should return '1st' for 1", () => { expect(getOrdinalNumber(1)).toEqual("1st"); }); + +// Case 2: Identify the ordinal number for 2 +test("should return '2nd' for 2", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); +}); + +// Case 3: Identify the ordinal number for 3 +test("should return '3rd' for 3", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); +}); + +// Case 4: Handle other numbers +test("should return 'th' for numbers other than 1, 2, or 3", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(10)).toEqual("10th"); +}); +// Case 5: Handle numbers ending in 1, 2, or 3 after 20 +test("should return '21st' for 21", () => { + expect(getOrdinalNumber(21)).toEqual("21st"); +}); + +test("should return '22nd' for 22", () => { + expect(getOrdinalNumber(22)).toEqual("22nd"); +}); + +test("should return '23rd' for 23", () => { + expect(getOrdinalNumber(23)).toEqual("23rd"); +}); diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..2942ab5a0 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,15 @@ -function repeat() { - return "hellohellohello"; +function repeat(str, count) { + if (count < 0) { + throw new Error("Count must be a non-negative integer"); + } + + let variable = ""; // inja natije ro jam mikonim + + for (let i = 0; i < count; i++) { + variable += str; + } + + return variable; } module.exports = repeat; diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 34097b09c..aad04e33b 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -21,12 +21,26 @@ test("should repeat the string count times", () => { // When the repeat 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", () => { + expect(repeat("hello", 1)).toEqual("hello"); +}); + // case: Handle Count of 0: // Given a target string str and a count equal to 0, // When the repeat 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", () => { + expect(repeat("hello", 0)).toEqual(""); +}); + // case: Negative Count: // Given a target string str and a negative integer count, // When the repeat 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 when count is negative", () => { + expect(() => { + repeat("hello", -1); + }).toThrow("Count must be a non-negative integer"); +});