From 07b6a4ff44f2108778eb6b3cc88bda3291fa9f1c Mon Sep 17 00:00:00 2001 From: HannaOdud Date: Mon, 27 Oct 2025 23:46:25 +0000 Subject: [PATCH 1/3] Sprint 3 practice tdd --- Sprint-3/2-practice-tdd/count.js | 9 +++++++- Sprint-3/2-practice-tdd/count.test.js | 12 ++++++++++ Sprint-3/2-practice-tdd/get-ordinal-number.js | 22 +++++++++++++++--- .../2-practice-tdd/get-ordinal-number.test.js | 23 +++++++++++++++++++ Sprint-3/2-practice-tdd/repeat.js | 15 ++++++++++-- Sprint-3/2-practice-tdd/repeat.test.js | 20 ++++++++++++++-- 6 files changed, 93 insertions(+), 8 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..341bf6968 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,12 @@ + function countChar(stringOfCharacters, findCharacter) { - return 5 + let total = 0; + for (let i=0; i < stringOfCharacters.length; i++){ + if (findCharacter == stringOfCharacters[i]){ + total++; + } + } + return total; } 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..8eef18c03 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -18,7 +18,19 @@ test("should count multiple occurrences of a character", () => { }); // Scenario: No Occurrences +test("No Occurrences", () => { + const str = "bcdefg"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); // 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("Case-sensitive str", () => { + const str = "Abcdefg"; + 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..4ff6bb7a8 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"; -} - + let result; + if (num % 100 == 11 || num % 100 == 12 || num % 100 == 13){ + result = num.toString() + "th"; + } + else if (num % 10 == 1){ + result = num.toString() +"st"; + } + else if (num % 10 == 2){ + result = num.toString() + "nd"; + } + else if (num % 10 == 3){ + result = num.toString() + "rd"; + } + else { + result = num.toString() + "th"; + } + + return result +} 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..ba6be1f01 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,26 @@ const getOrdinalNumber = require("./get-ordinal-number"); test("should return '1st' for 1", () => { expect(getOrdinalNumber(1)).toEqual("1st"); }); + +test("should return '2nd' for 2", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); +}); + +test("should return '3rd' for 3", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); +}); + +test("should return '4th' for 4", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); +}); +test("should return '11th' for 11", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); +}); + +test("should return '12th' for 12", () => { + expect(getOrdinalNumber(12)).toEqual("12th"); +}); + +test("should return '13th' for 13", () => { + expect(getOrdinalNumber(13)).toEqual("13th"); +}); diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..a69ccc0a8 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,16 @@ -function repeat() { - return "hellohellohello"; +function repeat(str, count) { + + if (count < 0){ + return null + } + else if (count == 0){ + return "" + } + let result = ""; + for ( let i=0; i { // Given a target string str and a count equal to 1, // 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("handle Count of 1", () => { + 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("handle Count of 0", () => { + 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("Negative Count", () => { + const str = "hello"; + const count = -1; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual(null); +}); \ No newline at end of file From d9fa7a99e5ca6d0bbec1f8af9cd6c915f63af700 Mon Sep 17 00:00:00 2001 From: HannaOdud Date: Mon, 3 Nov 2025 21:15:36 +0000 Subject: [PATCH 2/3] Some changes due to mentors review. --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 10 ++++++---- Sprint-3/2-practice-tdd/get-ordinal-number.test.js | 5 +++++ Sprint-3/2-practice-tdd/repeat.js | 8 ++++---- Sprint-3/2-practice-tdd/repeat.test.js | 5 +---- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index 4ff6bb7a8..d83882f35 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,15 +1,17 @@ function getOrdinalNumber(num) { let result; - if (num % 100 == 11 || num % 100 == 12 || num % 100 == 13){ + let rem_100 = num % 100; + let rem_10 = num % 10; + if (rem_100 == 11 || rem_100 == 12 || rem_100 == 13){ result = num.toString() + "th"; } - else if (num % 10 == 1){ + else if (rem_10 == 1){ result = num.toString() +"st"; } - else if (num % 10 == 2){ + else if (rem_10 == 2){ result = num.toString() + "nd"; } - else if (num % 10 == 3){ + else if (rem_10 == 3){ result = num.toString() + "rd"; } else { 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 ba6be1f01..2ebdb4048 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -34,3 +34,8 @@ test("should return '12th' for 12", () => { test("should return '13th' for 13", () => { expect(getOrdinalNumber(13)).toEqual("13th"); }); +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"); +}); diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index a69ccc0a8..a0226a96a 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,16 +1,16 @@ function repeat(str, count) { if (count < 0){ - return null + throw new Error("Count must be positive number!"); } else if (count == 0){ - return "" + return ""; } let result = ""; for ( let i=0; i { // 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("Negative Count", () => { - const str = "hello"; - const count = -1; - const repeatedStr = repeat(str, count); - expect(repeatedStr).toEqual(null); + expect(() => repeat("hello", -1)).toThrow("Count must be positive number!"); }); \ No newline at end of file From c17eaa5cbc24462ba475baec3fb72636051d302d Mon Sep 17 00:00:00 2001 From: HannaOdud Date: Wed, 5 Nov 2025 22:45:29 +0000 Subject: [PATCH 3/3] Some refactoring --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 14 ++++----- .../2-practice-tdd/get-ordinal-number.test.js | 31 +++++++++++-------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index d83882f35..548cd9515 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,23 +1,23 @@ function getOrdinalNumber(num) { let result; - let rem_100 = num % 100; - let rem_10 = num % 10; - if (rem_100 == 11 || rem_100 == 12 || rem_100 == 13){ + let lastTwoDigits = num % 100; + let lastDigit = num % 10; + if (lastTwoDigits == 11 || lastTwoDigits == 12 || lastTwoDigits == 13){ result = num.toString() + "th"; } - else if (rem_10 == 1){ + else if (lastDigit == 1){ result = num.toString() +"st"; } - else if (rem_10 == 2){ + else if (lastDigit == 2){ result = num.toString() + "nd"; } - else if (rem_10 == 3){ + else if (lastDigit == 3){ result = num.toString() + "rd"; } else { result = num.toString() + "th"; } - return result + return result; } 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 2ebdb4048..3d31d3d78 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -8,21 +8,30 @@ const getOrdinalNumber = require("./get-ordinal-number"); // When the number is 1, // Then the function should return "1st" -test("should return '1st' for 1", () => { - expect(getOrdinalNumber(1)).toEqual("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(131) ).toEqual("131st"); }); -test("should return '2nd' for 2", () => { - expect(getOrdinalNumber(2)).toEqual("2nd"); +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("should return '3rd' for 3", () => { - expect(getOrdinalNumber(3)).toEqual("3rd"); +test("append 'rd' to numbers ending in 3, except those ending in 13", () => { + expect( getOrdinalNumber(3) ).toEqual("3rd"); + expect( getOrdinalNumber(23) ).toEqual("23rd"); + expect( getOrdinalNumber(133) ).toEqual("133rd"); }); -test("should return '4th' for 4", () => { - expect(getOrdinalNumber(4)).toEqual("4th"); +test("append 'th' to numbers ending in 4 and more", () => { + expect( getOrdinalNumber(4) ).toEqual("4th"); + expect( getOrdinalNumber(25) ).toEqual("25th"); + expect( getOrdinalNumber(139) ).toEqual("139th"); }); + test("should return '11th' for 11", () => { expect(getOrdinalNumber(11)).toEqual("11th"); }); @@ -34,8 +43,4 @@ test("should return '12th' for 12", () => { test("should return '13th' for 13", () => { expect(getOrdinalNumber(13)).toEqual("13th"); }); -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"); -}); +