From eac7d73d269d204d4a5dea872ece0bb4dc8de33a Mon Sep 17 00:00:00 2001 From: Fattouma Ouannassi Date: Fri, 27 Feb 2026 01:00:40 +0000 Subject: [PATCH 1/7] update count.js --- Sprint-3/2-practice-tdd/count.js | 10 ++- Sprint-3/2-practice-tdd/count.test.js | 34 ++++++++-- Sprint-3/2-practice-tdd/get-ordinal-number.js | 15 ++++- .../2-practice-tdd/get-ordinal-number.test.js | 63 +++++++++++++------ Sprint-3/2-practice-tdd/repeat-str.js | 7 ++- Sprint-3/2-practice-tdd/repeat-str.test.js | 55 ++++++++-------- 6 files changed, 129 insertions(+), 55 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..1afc228a9 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,11 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 -} + let count = 0; + for (let i = 0; i < stringOfCharacters.length; i++) { + if (stringOfCharacters[i] === 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 179ea0ddf..1974d2aa6 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -1,24 +1,48 @@ +// ═══════════════════════════════════════════════════════ +// 🎯 Personal Implementation: countChar Function Tests + +// Purpose: Test-first practice for counting character occurrences +// Style: Gherkin-style BDD comments + Jest assertions +// ═══════════════════════════════════════════════════════ + // implement a function countChar that counts the number of times a character occurs in a string const countChar = require("./count"); + +// ─────────────────────────────────────────────────────── +// 📋 Specification: // Given a string `str` and a single character `char` to search for, // When the countChar function is called with these inputs, -// Then it should: +// Then it should return the number of times `char` appears in `str`. +// ─────────────────────────────────────────────────────── + +// ═══════════════════════════════════════════════════════ +// 🧪 Test Scenarios +// ═══════════════════════════════════════════════════════ // Scenario: Multiple Occurrences +// ────────────────────────────── // Given the input string `str`, // And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'), // When the function is called with these inputs, // Then it should correctly count occurrences of `char`. - test("should count multiple occurrences of a character", () => { - const str = "aaaaa"; - const char = "a"; + const str = "aaaaa"; // 🎯 Input: repeated character + const char = "a"; // 🔍 Search for: 'a' const count = countChar(str, char); - expect(count).toEqual(5); + expect(count).toEqual(5); // ✅ Expect: 5 occurrences }); // Scenario: No Occurrences +// ───────────────────────── // Given the input string `str`, // And a character `char` that does not exist within `str`. // When the function is called with these inputs, // Then it should return 0, indicating that no occurrences of `char` were found. +test("should return 0 for no occurence", () => { + const str = "asdf"; // 🎯 Input: no 'l' present + const char = "l"; // 🔍 Search for: 'l' (not in string) + const count = countChar(str, char); + expect(count).toEqual(0); // ✅ Expect: 0 occurrences +}); + + diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db1..60da79a95 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,16 @@ function getOrdinalNumber(num) { - return "1st"; + let numberToString = String(num); + let numberLastDigit = numberToString.slice(-1); + let numberLast2Digits = numberToString.slice(-2); + + if (numberLastDigit === "1" && numberLast2Digits !== "11") + return numberToString + "st"; + if (numberLastDigit === "2" && numberLast2Digits !== "12") + return numberToString + "nd"; + if (numberLastDigit === "3" && numberLast2Digits !== "13") + return numberToString + "rd"; + return numberToString + "th"; } -module.exports = getOrdinalNumber; + +module.exports = getOrdinalNumber; \ No newline at end of file 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 adfa58560..617fa933b 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -1,20 +1,47 @@ + 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. - -// To ensure thorough testing, we need broad scenarios that cover all possible cases. -// Listing individual values, however, can quickly lead to an unmanageable number of test cases. -// Instead of writing tests for individual numbers, consider grouping all possible input values -// into meaningful categories. Then, select representative samples from each category to test. -// This approach improves coverage and makes our tests easier to maintain. - -// Case 1: Numbers ending with 1 (but not 11) -// When the number ends with 1, except those ending with 11, -// Then the function should return a string by appending "st" to the number. -test("should append 'st' for numbers ending with 1, except those ending with 11", () => { - expect(getOrdinalNumber(1)).toEqual("1st"); - expect(getOrdinalNumber(21)).toEqual("21st"); - expect(getOrdinalNumber(131)).toEqual("131st"); + +// ─────────────────────────────────────────────────────── +// 📋 Rule Summary: +// • 1 → "1st", 2 → "2nd", 3 → "3rd" (unless teen) +// • 11, 12, 13 → always "th" (teen exception) +// • All others → last digit determines suffix +// ─────────────────────────────────────────────────────── + +// ── Case 1: Numbers ending in 1 (excluding teens) → "st" ── +test("appends 'st' to numbers ending in 1, except 11", () => { + expect(getOrdinalNumber(1)).toEqual("1st"); // single digit + expect(getOrdinalNumber(21)).toEqual("21st"); // double digit + expect(getOrdinalNumber(131)).toEqual("131st");// triple digit +}); + +// ── Case 2: Numbers ending in 2 (excluding teens) → "nd" ── +test("appends 'nd' to numbers ending in 2, except 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(122)).toEqual("122nd"); +}); + +// ── Case 3: Numbers ending in 3 (excluding teens) → "rd" ── +test("appends 'rd' to numbers ending in 3, except 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(123)).toEqual("123rd"); +}); + +// ── Case 4: Default "th" suffix (teens + digits 0,4-9) ── +test("appends 'th' for teens (11-13) and digits 0,4-9", () => { + // Teen exceptions (critical edge case) + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(111)).toEqual("111th"); // larger teen + + // Default suffix digits + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(25)).toEqual("25th"); }); + + + diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 3838c7b00..4b3befe4f 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,5 +1,6 @@ -function repeatStr() { - return "hellohellohello"; +function repeatStr(str, count) { + if (count < 0) throw new Error("negative counts are not valid"); + return str.repeat(count); } -module.exports = repeatStr; +module.exports = repeatStr; \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index a3fc1196c..c8d569e1c 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -1,32 +1,37 @@ -// Implement a function repeatStr const repeatStr = require("./repeat-str"); -// Given a target string `str` and a positive integer `count`, -// When the repeatStr function is called with these inputs, -// Then it should: -// Case: handle multiple repetitions: -// Given a target string `str` and a positive integer `count` greater than 1, -// When the repeatStr function is called with these inputs, -// Then it should return a string that contains the original `str` repeated `count` times. +// ─────────────────────────────────────────────────────── +// Rule Summary: +// • count > 1 → str repeated count times ("hi", 3 → "hihihi") +// • count = 1 → original string unchanged +// • count = 0 → empty string "" +// • count < 0 → throw error (invalid input) +// ─────────────────────────────────────────────────────── -test("should repeat the string count times", () => { - const str = "hello"; - const count = 3; - const repeatedStr = repeatStr(str, count); - expect(repeatedStr).toEqual("hellohellohello"); +// ── Case 1: Multiple repetitions (count > 1) ── +test("repeats string correctly for count > 1", () => { + expect(repeatStr("hello", 3)).toEqual("hellohellohello"); + expect(repeatStr("a", 5)).toEqual("aaaaa"); + expect(repeatStr("xy", 2)).toEqual("xyxy"); }); -// Case: handle count of 1: -// Given a target string `str` and a `count` equal to 1, -// When the repeatStr function is called with these inputs, -// Then it should return the original `str` without repetition. +// ── Case 2: Single repetition (count = 1) ── +test("returns original string unchanged when count = 1", () => { + expect(repeatStr("bye", 1)).toEqual("bye"); + expect(repeatStr("x", 1)).toEqual("x"); + expect(repeatStr("test", 1)).toEqual("test"); +}); + +// ── Case 3: Zero repetition (count = 0) ── +test("returns empty string when count = 0", () => { + expect(repeatStr("no", 0)).toEqual(""); + expect(repeatStr("anything", 0)).toEqual(""); + expect(repeatStr("", 0)).toEqual(""); +}); -// Case: Handle count of 0: -// Given a target string `str` and a `count` equal to 0, -// When the repeatStr function is called with these inputs, -// Then it should return an empty string. +// ── Case 4: Invalid input (negative count) ── +test("throws error for negative count values", () => { + expect(() => repeatStr("str", -1)).toThrow("negative counts are not valid"); + expect(() => repeatStr("x", -10)).toThrow("negative counts are not valid"); +}); -// Case: Handle negative count: -// Given a target string `str` and a negative integer `count`, -// When the repeatStr function is called with these inputs, -// Then it should throw an error, as negative counts are not valid. From 0ccc61136252a5f00a3908d9d5540d7d762a6e64 Mon Sep 17 00:00:00 2001 From: Fattouma Ouannassi Date: Sat, 28 Feb 2026 01:04:19 +0000 Subject: [PATCH 2/7] Update count.js --- Sprint-3/2-practice-tdd/count.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 1afc228a9..6d200c527 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -5,7 +5,6 @@ function countChar(stringOfCharacters, findCharacter) { count++; } } - - return count; + return count; } module.exports = countChar; From 3741e77f74da5c21004f1db05f29c40d203f1f41 Mon Sep 17 00:00:00 2001 From: Fattouma Ouannassi Date: Sat, 28 Feb 2026 01:06:00 +0000 Subject: [PATCH 3/7] Update count.test.js --- Sprint-3/2-practice-tdd/count.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 1974d2aa6..f1edb7314 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -6,7 +6,7 @@ // ═══════════════════════════════════════════════════════ // implement a function countChar that counts the number of times a character occurs in a string -const countChar = require("./count"); +//const countChar = require("./count"); // ─────────────────────────────────────────────────────── // 📋 Specification: @@ -25,6 +25,7 @@ const countChar = require("./count"); // And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'), // When the function is called with these inputs, // Then it should correctly count occurrences of `char`. +const countChar = require("./count"); test("should count multiple occurrences of a character", () => { const str = "aaaaa"; // 🎯 Input: repeated character const char = "a"; // 🔍 Search for: 'a' From e1590f613bd7e0ae5b03894237eb91d3aac1e18d Mon Sep 17 00:00:00 2001 From: Fattouma Ouannassi Date: Sat, 28 Feb 2026 01:06:26 +0000 Subject: [PATCH 4/7] Update get-ordinal-number.js --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index 60da79a95..2e3502dbe 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -11,6 +11,4 @@ function getOrdinalNumber(num) { return numberToString + "rd"; return numberToString + "th"; } - - -module.exports = getOrdinalNumber; \ No newline at end of file +module.exports = getOrdinalNumber; From 42bb265936af5f3840c3e164ff40a4646d285363 Mon Sep 17 00:00:00 2001 From: Fattouma Ouannassi Date: Sat, 28 Feb 2026 01:07:13 +0000 Subject: [PATCH 5/7] Update get-ordinal-number.test.js --- Sprint-3/2-practice-tdd/get-ordinal-number.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 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 617fa933b..b6b1eb34b 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -1,5 +1,5 @@ -const getOrdinalNumber = require("./get-ordinal-number"); +//const getOrdinalNumber = require("./get-ordinal-number"); // ─────────────────────────────────────────────────────── // 📋 Rule Summary: @@ -7,7 +7,7 @@ const getOrdinalNumber = require("./get-ordinal-number"); // • 11, 12, 13 → always "th" (teen exception) // • All others → last digit determines suffix // ─────────────────────────────────────────────────────── - +const getOrdinalNumber = require("./get-ordinal-number"); // ── Case 1: Numbers ending in 1 (excluding teens) → "st" ── test("appends 'st' to numbers ending in 1, except 11", () => { expect(getOrdinalNumber(1)).toEqual("1st"); // single digit From 3eced1c3281e99e9f1f9a4e36752c3a004b7cc14 Mon Sep 17 00:00:00 2001 From: Fattouma Ouannassi Date: Sat, 28 Feb 2026 01:07:41 +0000 Subject: [PATCH 6/7] Update repeat-str.js --- Sprint-3/2-practice-tdd/repeat-str.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 4b3befe4f..d7b507f92 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -2,5 +2,4 @@ function repeatStr(str, count) { if (count < 0) throw new Error("negative counts are not valid"); return str.repeat(count); } - -module.exports = repeatStr; \ No newline at end of file +module.exports = repeatStr; From c70c8dd965bebb25101b580b2fdceb3cdbf0963c Mon Sep 17 00:00:00 2001 From: Fattouma Ouannassi Date: Sat, 28 Feb 2026 01:08:44 +0000 Subject: [PATCH 7/7] Update repeat-str.test.js --- Sprint-3/2-practice-tdd/repeat-str.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index c8d569e1c..0bc3e3505 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -1,4 +1,4 @@ -const repeatStr = require("./repeat-str"); +//const repeatStr = require("./repeat-str"); // ─────────────────────────────────────────────────────── // Rule Summary: @@ -7,7 +7,7 @@ const repeatStr = require("./repeat-str"); // • count = 0 → empty string "" // • count < 0 → throw error (invalid input) // ─────────────────────────────────────────────────────── - +const repeatStr = require("./repeat-str"); // ── Case 1: Multiple repetitions (count > 1) ── test("repeats string correctly for count > 1", () => { expect(repeatStr("hello", 3)).toEqual("hellohellohello");