From e921f3cc612c0ff1db720cce85a1803225c0abae Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 13 Oct 2026 15:56:22 +0100 Subject: [PATCH 01/12] changed code in attempt to work for the test.. needs fixing. --- practice-tdd/count.js | 6 ++++-- practice-tdd/count.test.js | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/practice-tdd/count.js b/practice-tdd/count.js index 5518c5712..e2b14c198 100644 --- a/practice-tdd/count.js +++ b/practice-tdd/count.js @@ -1,3 +1,5 @@ -export function countChar(stringOfCharacters, findCharacter) { - return 5 +export function countChar(str, char) { + let onlyCharacter = str.replace(/char/, ""); + + return onlyCharacter.length; } diff --git a/practice-tdd/count.test.js b/practice-tdd/count.test.js index cb3aaae59..db380714e 100644 --- a/practice-tdd/count.test.js +++ b/practice-tdd/count.test.js @@ -22,3 +22,10 @@ test("should count multiple occurrences of a character", () => { // 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 count 0 with no occurrences of a character", () => { + const str = "aaaaa"; + const char = "b"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); From 5dfa1debfd86957813e255022e890c8964033886 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 13 Oct 2026 16:16:55 +0100 Subject: [PATCH 02/12] Worked on code to ensure that if result is null it will show a 0 --- practice-tdd/count.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/practice-tdd/count.js b/practice-tdd/count.js index e2b14c198..4f95d5f20 100644 --- a/practice-tdd/count.js +++ b/practice-tdd/count.js @@ -1,5 +1,7 @@ export function countChar(str, char) { - let onlyCharacter = str.replace(/char/, ""); - - return onlyCharacter.length; + let charMatch = str.match(new RegExp(char, "g")); + if (charMatch === null) { + return 0; + } + return charMatch.length; } From f920f8cb15b8dbb81039e8c5f77750879a734d4a Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 13 Oct 2026 16:23:46 +0100 Subject: [PATCH 03/12] Made it so code will show st for 1 at end of num --- practice-tdd/get-ordinal-number.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/practice-tdd/get-ordinal-number.js b/practice-tdd/get-ordinal-number.js index 5c6291f5e..c40207b0f 100644 --- a/practice-tdd/get-ordinal-number.js +++ b/practice-tdd/get-ordinal-number.js @@ -1,3 +1,7 @@ export function getOrdinalNumber(num) { - return "1st"; + let number = num.toString(); + + if (number.slice(-1) === "1") { + return number + "st"; + } } From 33059ce081cf41faffbd4500c772eb17d2cf22f4 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 13 Oct 2026 16:34:04 +0100 Subject: [PATCH 04/12] add th to numbers 11-19 --- practice-tdd/get-ordinal-number.js | 11 +++++++---- practice-tdd/get-ordinal-number.test.js | 6 ++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/practice-tdd/get-ordinal-number.js b/practice-tdd/get-ordinal-number.js index c40207b0f..e7f1e3767 100644 --- a/practice-tdd/get-ordinal-number.js +++ b/practice-tdd/get-ordinal-number.js @@ -1,7 +1,10 @@ export function getOrdinalNumber(num) { - let number = num.toString(); - - if (number.slice(-1) === "1") { - return number + "st"; + let numString = num.toString(); + if (11 <= Number(numString) && 19 >= Number(numString)) { + return numString + "th"; + } else if (numString.slice(-2) === "11") { + return numString + "st"; + } else if (numString.slice(-1) === "1") { + return numString + "st"; } } diff --git a/practice-tdd/get-ordinal-number.test.js b/practice-tdd/get-ordinal-number.test.js index e23174a18..47be20918 100644 --- a/practice-tdd/get-ordinal-number.test.js +++ b/practice-tdd/get-ordinal-number.test.js @@ -18,3 +18,9 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); }); + +test("should append 'th' for numbers ending with 11-19, except those ending with 11", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); +}); From 126b724627b505d2d5204de84987be397ac9646e Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 13 Oct 2026 16:39:48 +0100 Subject: [PATCH 05/12] Added test for 2nd, and fixed th for numbers over 100 --- practice-tdd/get-ordinal-number.js | 6 +++--- practice-tdd/get-ordinal-number.test.js | 10 ++++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/practice-tdd/get-ordinal-number.js b/practice-tdd/get-ordinal-number.js index e7f1e3767..805ea1584 100644 --- a/practice-tdd/get-ordinal-number.js +++ b/practice-tdd/get-ordinal-number.js @@ -1,10 +1,10 @@ export function getOrdinalNumber(num) { let numString = num.toString(); - if (11 <= Number(numString) && 19 >= Number(numString)) { + if (11 <= Number(numString.slice(-2)) && 19 >= Number(numString.slice(-2))) { return numString + "th"; - } else if (numString.slice(-2) === "11") { - return numString + "st"; } else if (numString.slice(-1) === "1") { return numString + "st"; + } else if (numString.slice(-1) === "2") { + return numString + "nd"; } } diff --git a/practice-tdd/get-ordinal-number.test.js b/practice-tdd/get-ordinal-number.test.js index 47be20918..27c7bdc7e 100644 --- a/practice-tdd/get-ordinal-number.test.js +++ b/practice-tdd/get-ordinal-number.test.js @@ -19,8 +19,14 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" expect(getOrdinalNumber(131)).toEqual("131st"); }); -test("should append 'th' for numbers ending with 11-19, except those ending with 11", () => { +test("should append 'th' for numbers ending with 11-19", () => { expect(getOrdinalNumber(11)).toEqual("11th"); expect(getOrdinalNumber(12)).toEqual("12th"); - expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(113)).toEqual("113th"); +}); + +test("should append 'nd' for numbers ending with 2, except those ending with 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(132)).toEqual("132nd"); }); From e86b00a3518c718f7ca5bdf113c515e34aaba871 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 13 Oct 2026 16:50:46 +0100 Subject: [PATCH 06/12] added tests for negative numbers and code for -11 to -19 --- practice-tdd/get-ordinal-number.js | 8 ++++++++ practice-tdd/get-ordinal-number.test.js | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/practice-tdd/get-ordinal-number.js b/practice-tdd/get-ordinal-number.js index 805ea1584..69b5f9f96 100644 --- a/practice-tdd/get-ordinal-number.js +++ b/practice-tdd/get-ordinal-number.js @@ -2,9 +2,17 @@ export function getOrdinalNumber(num) { let numString = num.toString(); if (11 <= Number(numString.slice(-2)) && 19 >= Number(numString.slice(-2))) { return numString + "th"; + } else if ( + -11 >= Number(numString.slice(-2)) && + -19 <= Number(numString.slice(-2)) + ) { + return numString + "th"; } else if (numString.slice(-1) === "1") { return numString + "st"; } else if (numString.slice(-1) === "2") { return numString + "nd"; + } else if (numString.slice(-1) === "3") { + return numString + "rd"; } + return numString + "th"; } diff --git a/practice-tdd/get-ordinal-number.test.js b/practice-tdd/get-ordinal-number.test.js index 27c7bdc7e..33f87e04d 100644 --- a/practice-tdd/get-ordinal-number.test.js +++ b/practice-tdd/get-ordinal-number.test.js @@ -30,3 +30,21 @@ test("should append 'nd' for numbers ending with 2, except those ending with 12" expect(getOrdinalNumber(22)).toEqual("22nd"); expect(getOrdinalNumber(132)).toEqual("132nd"); }); + +test("should append 'rd' for numbers ending with 3, except those ending with 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(133)).toEqual("133rd"); +}); + +test("should append 'th' for all other numbers", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(25)).toEqual("25th"); + expect(getOrdinalNumber(137)).toEqual("137th"); +}); + +test("should be giving correct endings for negative numbers", () => { + expect(getOrdinalNumber(-2)).toEqual("-2nd"); + expect(getOrdinalNumber(-23)).toEqual("-23rd"); + expect(getOrdinalNumber(-137)).toEqual("-137th"); +}); From 380ae279da4f131b46180b2c2fdde148adcd17c0 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 13 Oct 2026 16:53:53 +0100 Subject: [PATCH 07/12] corrected count using .repeat() --- practice-tdd/repeat-str.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/practice-tdd/repeat-str.js b/practice-tdd/repeat-str.js index af273f3b0..8ccb555d0 100644 --- a/practice-tdd/repeat-str.js +++ b/practice-tdd/repeat-str.js @@ -1,5 +1,5 @@ -export function repeatStr() { +export function repeatStr(str, count) { // 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"; + return str.repeat(count); } From c0312445206d66b8a4b26f241c97943f92f84c4e Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 13 Oct 2026 16:54:32 +0100 Subject: [PATCH 08/12] added test for count of 1 --- practice-tdd/repeat-str.test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/practice-tdd/repeat-str.test.js b/practice-tdd/repeat-str.test.js index 8a7036102..e97f14703 100644 --- a/practice-tdd/repeat-str.test.js +++ b/practice-tdd/repeat-str.test.js @@ -20,6 +20,12 @@ test("should repeat the string count times", () => { // 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 repeat the string count times", () => { + const str = "hello"; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("hello"); +}); // Case: Handle count of 0: // Given a target string `str` and a `count` equal to 0, From 7ad95cf70e99977c57a9fb683428da8112314da6 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 13 Oct 2026 16:55:31 +0100 Subject: [PATCH 09/12] added test for count of 0 --- practice-tdd/repeat-str.test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/practice-tdd/repeat-str.test.js b/practice-tdd/repeat-str.test.js index e97f14703..b8476fc28 100644 --- a/practice-tdd/repeat-str.test.js +++ b/practice-tdd/repeat-str.test.js @@ -31,6 +31,12 @@ test("should repeat the string count times", () => { // 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 repeat the string count times", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual(""); +}); // Case: Handle negative count: // Given a target string `str` and a negative integer `count`, From f3d3d7c2c32fc487d8897f9b9ed537dfa524d56d Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 13 Oct 2026 17:06:51 +0100 Subject: [PATCH 10/12] Added code and test to throw an error.. it is at this time I was reading the task again and see I must not use .repeat.... here we go again then ^_^ --- practice-tdd/repeat-str.js | 3 +++ practice-tdd/repeat-str.test.js | 13 +++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/practice-tdd/repeat-str.js b/practice-tdd/repeat-str.js index 8ccb555d0..944dc0113 100644 --- a/practice-tdd/repeat-str.js +++ b/practice-tdd/repeat-str.js @@ -1,5 +1,8 @@ export function repeatStr(str, count) { // 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. + if (count < 0) { + throw new Error("negative counts are not valid"); + } return str.repeat(count); } diff --git a/practice-tdd/repeat-str.test.js b/practice-tdd/repeat-str.test.js index b8476fc28..e127bf278 100644 --- a/practice-tdd/repeat-str.test.js +++ b/practice-tdd/repeat-str.test.js @@ -8,8 +8,7 @@ import { repeatStr } from "./repeat-str.js"; // 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", () => { +test("should repeat the string count 3 times", () => { const str = "hello"; const count = 3; const repeatedStr = repeatStr(str, count); @@ -20,7 +19,7 @@ test("should repeat the string count times", () => { // 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 repeat the string count times", () => { +test("should repeat the string count 1 times", () => { const str = "hello"; const count = 1; const repeatedStr = repeatStr(str, count); @@ -31,7 +30,7 @@ test("should repeat the string count times", () => { // 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 repeat the string count times", () => { +test("should repeat the string count 0 times", () => { const str = "hello"; const count = 0; const repeatedStr = repeatStr(str, count); @@ -42,3 +41,9 @@ test("should repeat the string count times", () => { // 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 'negative counts are not valid'", () => { + const str = "hello"; + const count = -3; + expect(() => repeatedStr(str, count)).toThrow; + new Error("negative counts are not valid"); +}); From f1b93ea827feb8218041e2124cc5196ffe71e11d Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 13 Oct 2026 17:34:16 +0100 Subject: [PATCH 11/12] fixed the code to ensure no use of repeat was used by using "for" --- practice-tdd/repeat-str.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/practice-tdd/repeat-str.js b/practice-tdd/repeat-str.js index 944dc0113..36c13f5d9 100644 --- a/practice-tdd/repeat-str.js +++ b/practice-tdd/repeat-str.js @@ -4,5 +4,9 @@ export function repeatStr(str, count) { if (count < 0) { throw new Error("negative counts are not valid"); } - return str.repeat(count); + let result = ""; + for (let i = 0; i < count; i++) { + result += str; + } + return result; } From b15db6b37cb1c6c350ac40c55ce103d802c0f7ae Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Wed, 14 Oct 2026 14:42:46 +0100 Subject: [PATCH 12/12] Fixxed errors in my toThrow --- practice-tdd/repeat-str.test.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/practice-tdd/repeat-str.test.js b/practice-tdd/repeat-str.test.js index e127bf278..5093fb792 100644 --- a/practice-tdd/repeat-str.test.js +++ b/practice-tdd/repeat-str.test.js @@ -44,6 +44,7 @@ test("should repeat the string count 0 times", () => { test("should throw an error 'negative counts are not valid'", () => { const str = "hello"; const count = -3; - expect(() => repeatedStr(str, count)).toThrow; - new Error("negative counts are not valid"); + expect(() => repeatStr(str, count)).toThrow( + new Error("negative counts are not valid") + ); });