From a21fb5ea0b64f9a1deb9ad6dd14eaee10aae9ea9 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Fri, 31 Oct 2025 08:46:50 +0000 Subject: [PATCH 1/8] Implement a countChar function and test with jest --- Sprint-3/2-practice-tdd/count.js | 21 ++++++++++++++++++++- Sprint-3/2-practice-tdd/count.test.js | 19 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..d92d4dde2 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,24 @@ 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}` + ); +} +assertFunction(countChar("Ahmaaa hmaaa",'a'),6); +assertFunction(countChar("Ahmaaa hmaaa",'m'),2 ); +assertFunction(countChar("Ahmaaa hmaaa",'b'),0); +assertFunction(countChar("Ahmaaa hmaaa",'A'),1); +assertFunction(countChar("",'A'),0); \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 42baf4b4b..326a1155d 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,22 @@ 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 occurrences of b characters ",()=>{ + const str="Ahmad Hmedan"; + const char= "b"; + const count=countChar(str,char); + expect(count).toEqual(0); +}) + +test("should return 1 occurrence of A characters",()=>{ + const str = "Ahmad Hmedan"; + const char = "A"; + const count = countChar(str, char); + expect(count).toEqual(1); +}) +test("should return 2 occurrence of m characters",()=>{ + const str = "Ahmad Hmedan"; + const char = "m"; + const count = countChar(str, char); + expect(count).toEqual(2); +}) \ No newline at end of file From 1382dc5d2159914066f9b359f5bfd41a7ed1d85c Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Fri, 31 Oct 2025 08:51:28 +0000 Subject: [PATCH 2/8] Add two test to cover empty string and speciale characters --- Sprint-3/2-practice-tdd/count.js | 5 ----- Sprint-3/2-practice-tdd/count.test.js | 14 +++++++++++++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index d92d4dde2..400b0be18 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -17,8 +17,3 @@ function assertFunction(currentOutput,targetOutput) `expect ${currentOutput} to equal ${targetOutput}` ); } -assertFunction(countChar("Ahmaaa hmaaa",'a'),6); -assertFunction(countChar("Ahmaaa hmaaa",'m'),2 ); -assertFunction(countChar("Ahmaaa hmaaa",'b'),0); -assertFunction(countChar("Ahmaaa hmaaa",'A'),1); -assertFunction(countChar("",'A'),0); \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 326a1155d..d7861cf8c 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -40,4 +40,16 @@ test("should return 2 occurrence of m characters",()=>{ const char = "m"; const count = countChar(str, char); expect(count).toEqual(2); -}) \ No newline at end of file +}) +test("should return occurrence of any characters in empty string",()=>{ + const str = ""; + const char = "@"; + const count = countChar(str, char); + expect(count).toEqual(0); +}) +test("should return 1 occurrence of @ characters in empty string",()=>{ + const str = "Ahmadhm@gamil.com"; + const char = "@"; + const count = countChar(str, char); + expect(count).toEqual(1); +}) From 7172fd2e29fbc48d7870536488418398fe2b3095 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Fri, 31 Oct 2025 10:30:39 +0000 Subject: [PATCH 3/8] I have implemented a function called repeat and writen a few different tests to check my function --- Sprint-3/2-practice-tdd/repeat.js | 9 +++++++-- Sprint-3/2-practice-tdd/repeat.test.js | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..b6af6ec3d 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,10 @@ -function repeat() { - return "hellohellohello"; +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; } 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..162369b72 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -21,12 +21,31 @@ 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 0", () => { + const str = "CYF"; + const count = -2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("Invalid Input must be a positive number"); +}); From 6c50af1c262c29ef1377403114a9e031c509abaa Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Fri, 31 Oct 2025 11:08:00 +0000 Subject: [PATCH 4/8] Use built-in .repeat() method instead of manual implementation; add one more test and verify all tests with the new function --- Sprint-3/2-practice-tdd/repeat.js | 24 +++++++++++++++++++----- Sprint-3/2-practice-tdd/repeat.test.js | 14 +++++++++++++- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index b6af6ec3d..bd35ff03f 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,10 +1,24 @@ 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"); + // 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 162369b72..894d0013c 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -43,9 +43,21 @@ test("should return empty when the count is 0", () => { // 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 0", () => { +test("should return empty when the count is negative", () => { const str = "CYF"; const count = -2; const repeatedStr = repeat(str, count); expect(repeatedStr).toEqual("Invalid Input 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(""); +}); From 5813df0cee20248045c7d397361048233aa13b78 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Fri, 31 Oct 2025 19:50:54 +0000 Subject: [PATCH 5/8] Implement getOrdinalNumber function and add comprehensive Jest tests --- Sprint-3/2-practice-tdd/count.js | 23 +++++---- Sprint-3/2-practice-tdd/count.test.js | 26 +++++----- Sprint-3/2-practice-tdd/get-ordinal-number.js | 18 ++++++- .../2-practice-tdd/get-ordinal-number.test.js | 48 +++++++++++++++++++ 4 files changed, 89 insertions(+), 26 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 400b0be18..a324df5f9 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,19 +1,18 @@ function countChar(stringOfCharacters, findCharacter) { - let counter=0; - let index=stringOfCharacters.indexOf(findCharacter); - while(index!== -1) - { - counter++; - - index = stringOfCharacters.indexOf(findCharacter,index+1); - } - return counter; + 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, +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 d7861cf8c..564fcf058 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,34 +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 occurrences of b characters ",()=>{ - const str="Ahmad Hmedan"; - const char= "b"; - const count=countChar(str,char); +test("should return 0 occurrences of b characters ", () => { + const str = "Ahmad Hmedan"; + const char = "b"; + const count = countChar(str, char); expect(count).toEqual(0); -}) +}); -test("should return 1 occurrence of A characters",()=>{ +test("should return 1 occurrence of A characters", () => { const str = "Ahmad Hmedan"; const char = "A"; const count = countChar(str, char); expect(count).toEqual(1); -}) -test("should return 2 occurrence of m characters",()=>{ +}); +test("should return 2 occurrence of m characters", () => { const str = "Ahmad Hmedan"; const char = "m"; const count = countChar(str, char); expect(count).toEqual(2); -}) -test("should return occurrence of any characters in empty string",()=>{ +}); +test("should return occurrence of any characters in empty string", () => { const str = ""; const char = "@"; const count = countChar(str, char); expect(count).toEqual(0); -}) -test("should return 1 occurrence of @ characters in empty string",()=>{ +}); +test("should return 1 occurrence of @ characters in empty string", () => { 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..692a91de1 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -11,3 +11,51 @@ const getOrdinalNumber = require("./get-ordinal-number"); test("should return '1st' for 1", () => { expect(getOrdinalNumber(1)).toEqual("1st"); }); +// Case 2: Identify the ordinal number for 22 +// When the number is 22, +// Then the function should return 22nd" +test("should return '22nd' for 22", () => { + expect(getOrdinalNumber(22)).toEqual("22nd"); +}); +// Case 3: Identify the ordinal number for 73 +// When the number is 73, +// Then the function should return 73rd" + +test("should return '73rd' for 73", () => { + expect(getOrdinalNumber(73)).toEqual("73rd"); +}); + +// Case 4: Identify the ordinal number for 99 +// When the number is 99, +// Then the function should return 99th" + +test("should return '99th' for 99", () => { + expect(getOrdinalNumber(99)).toEqual("99th"); +}); + +// Case 5: Identify the ordinal number for number ends with 11,12, or 13 +// When the number is 111, +// Then the function should return 111th" +// When the number is 212, +// Then the function should return 212th" +// When the number is 413, +// Then the function should return 413th" + +test("should return '111th' for 111", () => { + expect(getOrdinalNumber(111)).toEqual("111th"); +}); +test("should return '212th' for 212", () => { + expect(getOrdinalNumber(212)).toEqual("212th"); +}); +test("should return '413th' for 413", () => { + expect(getOrdinalNumber(413)).toEqual("413th"); +}); + +//case 6 +// when is the number is 0 or negative number +test("should throw Invalid Input for negative numbers", () => { + expect(getOrdinalNumber(-5)).toEqual("Invalid Input"); +}); +test("should throw Invalid Input for 0", () => { + expect(getOrdinalNumber(0)).toEqual("Invalid Input"); +}); From 4369f3abea83361815953f2a2fc4a9eb205c2315 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Sat, 8 Nov 2025 14:09:28 +0000 Subject: [PATCH 6/8] change the test discription --- Sprint-3/2-practice-tdd/count.test.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 564fcf058..2836a838f 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,32 +22,32 @@ 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 occurrences of b characters ", () => { +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 return 1 occurrence of A characters", () => { +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 return 2 occurrence of m characters", () => { +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 occurrence of any characters in empty string", () => { +test("should return 0 when the string is empty", () => { const str = ""; const char = "@"; const count = countChar(str, char); expect(count).toEqual(0); }); -test("should return 1 occurrence of @ characters in empty string", () => { +test("should correctly count special characters", () => { const str = "Ahmadhm@gamil.com"; const char = "@"; const count = countChar(str, char); From 034d8f802d3be6d701eeafb884f72575695a5715 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Sat, 8 Nov 2025 14:32:34 +0000 Subject: [PATCH 7/8] grouping the tests by behaviour --- .../2-practice-tdd/get-ordinal-number.test.js | 62 ++++++------------- 1 file changed, 20 insertions(+), 42 deletions(-) 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 692a91de1..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,58 +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"); }); -// Case 2: Identify the ordinal number for 22 -// When the number is 22, -// Then the function should return 22nd" -test("should return '22nd' for 22", () => { - expect(getOrdinalNumber(22)).toEqual("22nd"); +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"); }); -// Case 3: Identify the ordinal number for 73 -// When the number is 73, -// Then the function should return 73rd" - -test("should return '73rd' for 73", () => { - expect(getOrdinalNumber(73)).toEqual("73rd"); +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"); }); -// Case 4: Identify the ordinal number for 99 -// When the number is 99, -// Then the function should return 99th" -test("should return '99th' for 99", () => { +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"); }); -// Case 5: Identify the ordinal number for number ends with 11,12, or 13 -// When the number is 111, -// Then the function should return 111th" -// When the number is 212, -// Then the function should return 212th" -// When the number is 413, -// Then the function should return 413th" -test("should return '111th' for 111", () => { - expect(getOrdinalNumber(111)).toEqual("111th"); -}); -test("should return '212th' for 212", () => { - expect(getOrdinalNumber(212)).toEqual("212th"); -}); -test("should return '413th' for 413", () => { - expect(getOrdinalNumber(413)).toEqual("413th"); -}); - -//case 6 -// when is the number is 0 or negative number -test("should throw Invalid Input for negative numbers", () => { +test("should throw Invalid Input for negative numbers or 0", () => { expect(getOrdinalNumber(-5)).toEqual("Invalid Input"); -}); -test("should throw Invalid Input for 0", () => { - expect(getOrdinalNumber(0)).toEqual("Invalid Input"); + expect(getOrdinalNumber(0)).toEqual("Invalid Input"); }); From 3be91c253f38fe45216a262a2854e6d9f72e2695 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Sat, 8 Nov 2025 15:35:19 +0000 Subject: [PATCH 8/8] resolve invalide input to throw a new error insted a return a massage --- Sprint-3/2-practice-tdd/repeat.js | 3 +-- Sprint-3/2-practice-tdd/repeat.test.js | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index bd35ff03f..32a9df7d6 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,6 +1,5 @@ function repeat(myString, repeatNumber) { - if (repeatNumber < 0) return "Invalid Input must be a positive number"; - //if (repeatNumber < 0) throw new Error("Repeat count must be a positive number"); + 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); } diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 894d0013c..ee54c80cd 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -46,8 +46,8 @@ test("should return empty when the count is 0", () => { test("should return empty when the count is negative", () => { const str = "CYF"; const count = -2; - const repeatedStr = repeat(str, count); - expect(repeatedStr).toEqual("Invalid Input must be a positive number"); + + 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";