From f5fe018b5a843a724ae9c65aec6908653ffdb16b Mon Sep 17 00:00:00 2001 From: KhotKeys Date: Mon, 27 Jul 2026 22:37:56 +0100 Subject: [PATCH 1/3] Implement countChar with TDD --- Sprint-3/2-practice-tdd/count.js | 6 +++++- Sprint-3/2-practice-tdd/count.test.js | 23 ++++------------------- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..2e199b488a 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,9 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + for (const char of stringOfCharacters) { + if (char === 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 179ea0ddf7..25cab9fce9 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -1,24 +1,9 @@ -// implement a function countChar that counts the number of times a character occurs in a string const countChar = require("./count"); -// Given a string `str` and a single character `char` to search for, -// When the countChar function is called with these inputs, -// Then it should: - -// 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 count = countChar(str, char); - expect(count).toEqual(5); + expect(countChar("aaaaa", "a")).toEqual(5); }); -// 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 when character does not occur", () => { + expect(countChar("hello", "z")).toEqual(0); +}); From 1526faf79f106ff9d9f29b18ca70c4612b5690a7 Mon Sep 17 00:00:00 2001 From: KhotKeys Date: Mon, 27 Jul 2026 22:38:21 +0100 Subject: [PATCH 2/3] Implement repeatStr with TDD --- Sprint-3/2-practice-tdd/repeat-str.js | 9 +++--- Sprint-3/2-practice-tdd/repeat-str.test.js | 35 +++++++--------------- 2 files changed, 15 insertions(+), 29 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 2af0a2cea7..3929b97c43 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,7 +1,8 @@ -function repeatStr() { - // Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat). - // The goal is to re-implement that function, not to use it. - return "hellohellohello"; +function repeatStr(str, count) { + if (count < 0) throw new Error("count must be non-negative"); + let result = ""; + for (let i = 0; i < count; i++) result += str; + return result; } module.exports = repeatStr; diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index a3fc1196c4..328679c431 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -1,32 +1,17 @@ -// 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. test("should repeat the string count times", () => { - const str = "hello"; - const count = 3; - const repeatedStr = repeatStr(str, count); - expect(repeatedStr).toEqual("hellohellohello"); + expect(repeatStr("hello", 3)).toEqual("hellohellohello"); }); -// 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. +test("should return the original string when count is 1", () => { + expect(repeatStr("hello", 1)).toEqual("hello"); +}); -// 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. +test("should return empty string when count is 0", () => { + expect(repeatStr("hello", 0)).toEqual(""); +}); -// 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. +test("should throw an error when count is negative", () => { + expect(() => repeatStr("hello", -1)).toThrow(); +}); From 47d8723fd0f083900ecff4f188b0a32b2074e97f Mon Sep 17 00:00:00 2001 From: KhotKeys Date: Mon, 27 Jul 2026 22:38:41 +0100 Subject: [PATCH 3/3] Implement getOrdinalNumber with TDD --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db13..a7f38ad328 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,11 @@ function getOrdinalNumber(num) { - return "1st"; + const lastTwo = num % 100; + const lastOne = num % 10; + if (lastTwo >= 11 && lastTwo <= 13) return `${num}th`; + if (lastOne === 1) return `${num}st`; + if (lastOne === 2) return `${num}nd`; + if (lastOne === 3) return `${num}rd`; + return `${num}th`; } module.exports = getOrdinalNumber;