diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..6a04c9107 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 character of stringOfCharacters) { + if (character === 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..763af4c8f 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -17,8 +17,28 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(5); }); +test("should count multiple occurrences of a character", () => { + const str = "banana"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(3); +}); + +test("should count multiple occurrences of a character", () => { + const str = "ananas"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(3); +}); + // Scenario: No Occurrences // Given the input string str, // 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 count no occurrence of a character", () => { + const str = "hello"; + const char = "a"; + 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 f95d71db1..4f5153e2f 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,12 @@ function getOrdinalNumber(num) { - return "1st"; + const last = num % 10; + const lastTwo = num % 100; + + if (last === 1 && lastTwo !== 11) return num + "st"; + if (last === 2 && lastTwo !== 12) return num + "nd"; + if (last === 3 && lastTwo !== 13) 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..8736a1508 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -7,7 +7,33 @@ const getOrdinalNumber = require("./get-ordinal-number"); // Case 1: Identify the ordinal number for 1 // When the number is 1, // Then the function should return "1st" +test("append 'st' to numbers ending in 1, except those ending in 11", () => { + expect( getOrdinalNumber(1) ).toEqual("1st"); + expect( getOrdinalNumber(21) ).toEqual("21st"); + expect( getOrdinalNumber(221) ).toEqual("221st"); + expect( getOrdinalNumber(11) ).toEqual("11th"); +}); + +test("append 'nd' to numbers ending in 2, except those ending in 12 ", () => { + expect( getOrdinalNumber(2) ).toEqual("2nd"); + expect( getOrdinalNumber(22) ).toEqual("22nd"); + expect( getOrdinalNumber(132) ).toEqual("132nd"); + expect( getOrdinalNumber(12) ).toEqual("12th"); +}); -test("should return '1st' for 1", () => { - expect(getOrdinalNumber(1)).toEqual("1st"); +test("append 'rd' to numbers ending in 3, except those ending in 13", () => { + expect( getOrdinalNumber(3) ).toEqual("3rd"); + expect( getOrdinalNumber(33) ).toEqual("33rd"); + expect( getOrdinalNumber(133) ).toEqual("133rd"); + expect( getOrdinalNumber(13) ).toEqual("13th"); }); + +test("appends 'th' to numbers ending in 4 - 9, and 0 ", () => { + expect(getOrdinalNumber(4)).toBe("4th"); + expect(getOrdinalNumber(15)).toBe("15th"); + expect(getOrdinalNumber(36)).toBe("36th"); + expect(getOrdinalNumber(57)).toBe("57th"); + expect(getOrdinalNumber(78)).toBe("78th"); + expect(getOrdinalNumber(10)).toBe("10th"); +}); + diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..a1ed576da 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,13 @@ -function repeat() { - return "hellohellohello"; +function repeat(word, n) { + if (typeof n !== 'number' || n < 0) { + throw new Error("Count must be a non-negative integer"); + } + if (n === 0) { + return ""; + } + return word.repeat(n); } 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..09cd7da39 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -21,12 +21,30 @@ 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 repeat the string count times", () => { + const str = "hello"; + const count = 1; + const repeatedStr = repeat(str, count); + expect(repeatedStr).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 repeat the string count times", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeat(str, count); + expect(repeatedStr).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 repeat the string count times", () => { + const str = "hello"; + const count = -1; + expect(() => repeat(str, count)).toThrow("Count must be a non-negative integer"); +});