diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..a324df5f9 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,18 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let counter = 0; + let index = stringOfCharacters.indexOf(findCharacter); + while (index !== -1) { + counter++; + + index = stringOfCharacters.indexOf(findCharacter, index + 1); + } + return counter; } module.exports = countChar; +function assertFunction(currentOutput, targetOutput) { + console.assert( + currentOutput === targetOutput, + `expect ${currentOutput} to equal ${targetOutput}` + ); +} diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 42baf4b4b..2836a838f 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,34 @@ 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 the character does not exist in the string ", () => { + const str = "Ahmad Hmedan"; + const char = "b"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +test("should count a single occurrence when the character appears once",() => { + const str = "Ahmad Hmedan"; + const char = "A"; + const count = countChar(str, char); + expect(count).toEqual(1); +}); +test("should count multiple occurrences when the character appears more than once", () => { + const str = "Ahmad Hmedan"; + const char = "m"; + const count = countChar(str, char); + expect(count).toEqual(2); +}); +test("should return 0 when the string is empty", () => { + const str = ""; + const char = "@"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); +test("should correctly count special characters", () => { + const str = "Ahmadhm@gamil.com"; + const char = "@"; + const count = countChar(str, char); + expect(count).toEqual(1); +}); diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db1..2a21376b4 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,21 @@ function getOrdinalNumber(num) { - return "1st"; + if (num <= 0) return "Invalid Input"; + if (num % 100 === 11) return `${num}th`; // check the last two digit if it exception 11, 12 or 13 + if (num % 100 === 12) return `${num}th`; + if (num % 100 === 13) return `${num}th`; + const remainder = num % 10; + switch (remainder) { + case 1: + return `${num}st`; + + case 2: + return `${num}nd`; + case 3: + return `${num}rd`; + + default: + 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..cbf20cf47 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,36 @@ 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" -test("should return '1st' for 1", () => { + +test("append 'st' to numbers ending in 1, except those ending in 11", () => { expect(getOrdinalNumber(1)).toEqual("1st"); + expect(getOrdinalNumber(221)).toEqual("221st"); + expect(getOrdinalNumber(191)).toEqual("191st"); +}); +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"); +}); +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"); +}); + + +test("append 'th'to numbers ending in 4-9 or ending with 11,12,13", () => { + expect(getOrdinalNumber(99)).toEqual("99th"); + expect(getOrdinalNumber(234)).toEqual("234th"); + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(112)).toEqual("112th"); + expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(313)).toEqual("313th"); +}); + + +test("should throw Invalid Input for negative numbers or 0", () => { + expect(getOrdinalNumber(-5)).toEqual("Invalid Input"); + expect(getOrdinalNumber(0)).toEqual("Invalid Input"); }); diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..32a9df7d6 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,23 @@ -function repeat() { - return "hellohellohello"; +function repeat(myString, repeatNumber) { + if (repeatNumber < 0) throw new Error("Repeat count must be a positive number"); + // if I use this instead return how I can test it with jest?? + return myString.repeat(repeatNumber); } module.exports = repeat; + +// Note: +// When I wrote this code, I didn’t know there was already a built-in method for this. +// Please review this implementation as well. +//if (repeatNumber < 0) throw new Error("Repeat count must be a positive number"); if I use this instead return how I can test it with jest?? + +// function repeat(myString, repeatNumber) { +// if (repeatNumber < 0) return "Invalid Input must be a positive number"; +// let str = ""; +// for (let i = 0; i < repeatNumber; i++) { +// str += myString; +// } +// return str; +// } + +//if (repeatNumber < 0) throw new Error("Repeat count must be a positive number"); diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 34097b09c..ee54c80cd 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -21,12 +21,43 @@ 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 string", () => { + const str = "CYF"; + const count = 1; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("CYF"); +}); + // 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 empty when the count is 0", () => { + const str = "CYF"; + 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 return empty when the count is negative", () => { + const str = "CYF"; + const count = -2; + + expect(() => repeat(str,count)).toThrow("Repeat count must be a positive number"); +}); +test("should repeat the string five times when the count is 5", () => { + const str = "CYF"; + const count = 5; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("CYFCYFCYFCYFCYF"); +}); +test("should return an empty string when the str is empty whatever was count equal", () => { + const str = ""; + const count = 10; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual(""); +});