From 19a965cd4ff240c2d640d003c9182dbc2795817d Mon Sep 17 00:00:00 2001 From: Isaac Abodunrin Date: Sat, 14 Feb 2026 10:33:33 +0200 Subject: [PATCH 01/13] Ordinal number solution --- Sprint-3/2-practice-tdd/get-ordinal-number.test.js | 8 ++++++++ 1 file changed, 8 insertions(+) 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..8d1047433 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -18,3 +18,11 @@ 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", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(700011)).toEqual("700011th"); +}); + +test(); From e918829329e485695890f271f9766e69ac5e303c Mon Sep 17 00:00:00 2001 From: Isaac Abodunrin Date: Sun, 15 Feb 2026 18:22:18 +0200 Subject: [PATCH 02/13] Test for multiple occurences of a character --- Sprint-3/2-practice-tdd/count.test.js | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf..a3327c941 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -1,20 +1,12 @@ // 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`. +// Scenario 1: Multiple Occurrences 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); + expect(countChar("sea saw", "s")).toEqual(2); + expect(countChar("Arrested Development", "d")).toEqual(2); + expect(countChar("* Star * TV *", "*")).toEqual(3); }); // Scenario: No Occurrences From a4f590bc3e19f4f0e9d2b5926ea073103318d2ff Mon Sep 17 00:00:00 2001 From: Isaac Abodunrin Date: Sun, 15 Feb 2026 18:26:25 +0200 Subject: [PATCH 03/13] Test for single occurence of a character --- Sprint-3/2-practice-tdd/count.test.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index a3327c941..70d54be5e 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -9,6 +9,14 @@ test("should count multiple occurrences of a character", () => { expect(countChar("* Star * TV *", "*")).toEqual(3); }); +// Scenario 2: Single Occurrence +test("should count multiple occurrences of a character", () => { + expect(countChar("a", "a")).toEqual(1); + expect(countChar("Star Light", "i")).toEqual(1); + expect(countChar("!@#$%^&*()", "#")).toEqual(1); + expect(countChar("onomatopoeia", "t")).toEqual(1); +}); + // Scenario: No Occurrences // Given the input string `str`, // And a character `char` that does not exist within `str`. From 7816f8b74b56ca00faa4ddf500da6bc7a42bc75c Mon Sep 17 00:00:00 2001 From: Isaac Abodunrin Date: Sun, 15 Feb 2026 18:31:57 +0200 Subject: [PATCH 04/13] Test for no occurences of a character --- Sprint-3/2-practice-tdd/count.test.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 70d54be5e..768d8e633 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -10,15 +10,17 @@ test("should count multiple occurrences of a character", () => { }); // Scenario 2: Single Occurrence -test("should count multiple occurrences of a character", () => { +test("should count single occurrence of a character", () => { expect(countChar("a", "a")).toEqual(1); expect(countChar("Star Light", "i")).toEqual(1); expect(countChar("!@#$%^&*()", "#")).toEqual(1); expect(countChar("onomatopoeia", "t")).toEqual(1); }); -// 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. +// Scenario 3: No Occurrence +test("should count single occurrence of a character", () => { + expect(countChar("The Rookie", "S")).toEqual(0); + expect(countChar("Angela Bassett", "r")).toEqual(0); + expect(countChar("Angela Bassett", "r")).toEqual(0); + expect(countChar("Better Off Ted", "9")).toEqual(0); +}); From 21048fe95b5d33a0db66652fa7b2dd682a8010c6 Mon Sep 17 00:00:00 2001 From: Isaac Abodunrin Date: Tue, 17 Feb 2026 21:55:49 +0200 Subject: [PATCH 05/13] Tests for zero, single, and multuple occurrences. Invvalid edge case tests included --- Sprint-3/2-practice-tdd/count.test.js | 55 +++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 768d8e633..34b1be125 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -24,3 +24,58 @@ test("should count single occurrence of a character", () => { expect(countChar("Angela Bassett", "r")).toEqual(0); expect(countChar("Better Off Ted", "9")).toEqual(0); }); + +// Scenario 4: Invalid Entries +test(`Should throw an error when given an invalid input`, () => { + expect(() => { + countChar("Courage, the Cowardly Dog", "dog"); + }).toThrow(); +}); + +test(`Should throw an error when given an invalid input`, () => { + expect(() => { + countChar("Sheep in the Big City", 1); + }).toThrow(); +}); + +test(`Should throw an error when given an invalid input`, () => { + expect(() => { + countChar(-205, "a"); + }).toThrow(); +}); + +test(`Should throw an error when given an invalid input`, () => { + expect(() => { + countChar(-205, 55.5); + }).toThrow(); +}); + +test(`Should throw an error when given an invalid input`, () => { + expect(() => { + countChar(true, "f"); + }).toThrow(); +}); + +test(`Should throw an error when given an invalid input`, () => { + expect(() => { + countChar(undefined, "f"); + }).toThrow(); +}); + +test(`Should throw an error when given an invalid input`, () => { + expect(() => { + countChar(null, true); + }).toThrow(); +}); + +test(`Should throw an error when given an invalid input`, () => { + expect(() => { + countChar(false, true); + }).toThrow(); +}); + +test(`Should throw an error when given an invalid input`, () => { + expect(() => { + countChar([false], {}); + }).toThrow(); +}); From 6364ddd817fbe39ed5df853b680858534aa1cb43 Mon Sep 17 00:00:00 2001 From: Isaac Abodunrin Date: Tue, 17 Feb 2026 22:02:41 +0200 Subject: [PATCH 06/13] Count.js function --- Sprint-3/2-practice-tdd/count.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..bc13deb4c 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,30 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + if ( + typeof stringOfCharacters != "string" || + typeof findCharacter != "string" + ) { + throw new Error("Please enter a valid string"); + } + + if (findCharacter.length != 1) { + throw new Error( + "Please enter a single character for findCharacter, e.g. 't" + ); + } + + if (stringOfCharacters.length === 0) { + throw new Error( + "Please enter a non-empty string, e.g. 'A series of unfortunate events'" + ); + } + + let charCount = 0; + let lowerCaseChar = findCharacter.toLowerCase(); + for (char of stringOfCharacters.toLowerCase()) { + if (lowerCaseChar === char) charCount++; + } + + return charCount; } module.exports = countChar; From e8a096f896b16d9d90788bdb176efd934ac5c54e Mon Sep 17 00:00:00 2001 From: Isaac Abodunrin Date: Tue, 17 Feb 2026 22:50:38 +0200 Subject: [PATCH 07/13] Refactor invalid entry testing using describe --- Sprint-3/2-practice-tdd/count.test.js | 70 +++++++-------------------- 1 file changed, 18 insertions(+), 52 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 34b1be125..7773ef405 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -26,56 +26,22 @@ test("should count single occurrence of a character", () => { }); // Scenario 4: Invalid Entries -test(`Should throw an error when given an invalid input`, () => { - expect(() => { - countChar("Courage, the Cowardly Dog", "dog"); - }).toThrow(); -}); - -test(`Should throw an error when given an invalid input`, () => { - expect(() => { - countChar("Sheep in the Big City", 1); - }).toThrow(); -}); - -test(`Should throw an error when given an invalid input`, () => { - expect(() => { - countChar(-205, "a"); - }).toThrow(); -}); - -test(`Should throw an error when given an invalid input`, () => { - expect(() => { - countChar(-205, 55.5); - }).toThrow(); -}); - -test(`Should throw an error when given an invalid input`, () => { - expect(() => { - countChar(true, "f"); - }).toThrow(); -}); - -test(`Should throw an error when given an invalid input`, () => { - expect(() => { - countChar(undefined, "f"); - }).toThrow(); -}); - -test(`Should throw an error when given an invalid input`, () => { - expect(() => { - countChar(null, true); - }).toThrow(); -}); - -test(`Should throw an error when given an invalid input`, () => { - expect(() => { - countChar(false, true); - }).toThrow(); -}); - -test(`Should throw an error when given an invalid input`, () => { - expect(() => { - countChar([false], {}); - }).toThrow(); +const invalidEntries = [ + { a: "Courage, the Cowardly Dog", b: "dog" }, + { a: "Sheep in the Big City", b: 1 }, + { a: -205, b: "a" }, + { a: -205, b: 55.5 }, + { a: true, b: "f" }, + { a: undefined, b: "f" }, + { a: null, b: true }, + { a: false, b: true }, + { a: [false], b: {} }, +]; + +describe.each(invalidEntries)("countChar($a, $b)", ({ a, b }) => { + test(`Should throw an error when given an invalid input (${a}, ${b})`, () => { + expect(() => { + countChar(a, b); + }).toThrow(); + }); }); From 483e8fdf5a547b8d8cb5754458b7da50f35247af Mon Sep 17 00:00:00 2001 From: Isaac Abodunrin Date: Wed, 18 Feb 2026 23:34:06 +0200 Subject: [PATCH 08/13] Refactor test for readbility --- Sprint-3/2-practice-tdd/count.test.js | 79 ++++++++++++++++----------- 1 file changed, 48 insertions(+), 31 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 7773ef405..a912eca6f 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -1,47 +1,64 @@ -// implement a function countChar that counts the number of times a character occurs in a string const countChar = require("./count"); // Scenario 1: Multiple Occurrences -test("should count multiple occurrences of a character", () => { - expect(countChar("aaaaa", "a")).toEqual(5); - expect(countChar("sea saw", "s")).toEqual(2); - expect(countChar("Arrested Development", "d")).toEqual(2); - expect(countChar("* Star * TV *", "*")).toEqual(3); +test("Should count multiple occurrences of a character", () => { + const multipleOccurrences = [ + { a: "aaaaa", b: "a", c: 5 }, + { a: "sea saw", b: "s", c: 2 }, + { a: "Arrested Development", b: "d", c: 2 }, + { a: "* Star * TV *", b: "*", c: 3 }, + ]; + + for (const { a, b, c } of multipleOccurrences) { + expect(countChar(a, b)).toEqual(c); + } }); // Scenario 2: Single Occurrence -test("should count single occurrence of a character", () => { - expect(countChar("a", "a")).toEqual(1); - expect(countChar("Star Light", "i")).toEqual(1); - expect(countChar("!@#$%^&*()", "#")).toEqual(1); - expect(countChar("onomatopoeia", "t")).toEqual(1); +test("Should count single occurrence of a character", () => { + const singleOccurrences = [ + { a: "a", b: "a" }, + { a: "Star Light", b: "i" }, + { a: "!@#$%^&*()", b: "#" }, + { a: "Why fly?", b: " " }, + ]; + + for (const { a, b } of singleOccurrences) { + expect(countChar(a, b)).toEqual(1); + } }); // Scenario 3: No Occurrence -test("should count single occurrence of a character", () => { - expect(countChar("The Rookie", "S")).toEqual(0); - expect(countChar("Angela Bassett", "r")).toEqual(0); - expect(countChar("Angela Bassett", "r")).toEqual(0); - expect(countChar("Better Off Ted", "9")).toEqual(0); +test("Should return 0 for no occurrence of a character", () => { + const noOccurrences = [ + { a: "The Rookie", b: "S" }, + { a: "Angela Bassett", b: "r" }, + { a: "Santa Clarita Diet", b: "*" }, + { a: "Better Off Ted", b: "9" }, + ]; + + for (const { a, b } of noOccurrences) { + expect(countChar(a, b)).toEqual(0); + } }); // Scenario 4: Invalid Entries -const invalidEntries = [ - { a: "Courage, the Cowardly Dog", b: "dog" }, - { a: "Sheep in the Big City", b: 1 }, - { a: -205, b: "a" }, - { a: -205, b: 55.5 }, - { a: true, b: "f" }, - { a: undefined, b: "f" }, - { a: null, b: true }, - { a: false, b: true }, - { a: [false], b: {} }, -]; - -describe.each(invalidEntries)("countChar($a, $b)", ({ a, b }) => { - test(`Should throw an error when given an invalid input (${a}, ${b})`, () => { +test("Should throw an error when given an invalid input", () => { + const invalidEntries = [ + { a: "Courage, the Cowardly Dog", b: "dog" }, + { a: "Sheep in the Big City", b: 1 }, + { a: -205, b: "a" }, + { a: -205, b: 55.5 }, + { a: true, b: "f" }, + { a: undefined, b: "f" }, + { a: null, b: true }, + { a: false, b: true }, + { a: [false], b: {} }, + ]; + + for (const { a, b } of invalidEntries) { expect(() => { countChar(a, b); }).toThrow(); - }); + } }); From 54a5617175acd31595bb3a87ce1f57b058dce761 Mon Sep 17 00:00:00 2001 From: Isaac Abodunrin Date: Thu, 19 Feb 2026 09:04:19 +0200 Subject: [PATCH 09/13] Testing: ordinal numbers --- .../2-practice-tdd/get-ordinal-number.test.js | 135 +++++++++++++++--- 1 file changed, 115 insertions(+), 20 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 8d1047433..1c71924b9 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -1,28 +1,123 @@ 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. +// Case 1: Numbers ending with 1 (but not in 11) 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"); + const numbers = [1, 21, 131]; + + for (const number of numbers) { + expect(getOrdinalNumber(number)).toEqual(`${number}st`); + } }); +// Case 2: Numbers ending with 11 test("should append 'th' for numbers ending with 11", () => { - expect(getOrdinalNumber(11)).toEqual("11th"); - expect(getOrdinalNumber(11)).toEqual("11th"); - expect(getOrdinalNumber(700011)).toEqual("700011th"); + const numbers = [11, 111, 700011]; + + for (const number of numbers) { + expect(getOrdinalNumber(number)).toEqual(`${number}th`); + } +}); + +// Case 3: Numbers ending with 2 (but not in 12) +test("should append 'nd' for numbers ending in 2", () => { + const numbers = [2, 22, 32, 502]; + + for (const number of numbers) { + expect(getOrdinalNumber(number)).toEqual(`${number}nd`); + } }); -test(); +// Case 4: Numbers ending with 12 +test("should append 'th' for numbers ending in 12", () => { + const numbers = [12, 212, 312, 5012]; + + for (const number of numbers) { + expect(getOrdinalNumber(number)).toEqual(`${number}th`); + } +}); + +// Case 5: Numbers ending in 3 (but not in 13) +test("should append 'rd' for numbers ending in 3 (but not 13)", () => { + const numbers = [3, 23, 33, 1063]; + + for (const number of numbers) { + expect(getOrdinalNumber(number)).toEqual(`${number}rd`); + } +}); + +// Case 6: Numbers ending in 13 +test("should append 'th' for numbers ending in 13", () => { + const numbers = [13, 113, 613, 713]; + + for (const number of numbers) { + expect(getOrdinalNumber(number)).toEqual(`${number}th`); + } +}); + +// Case 7: Numbers ending in 4-10 +test("should append 'th' for numbers ending in 4-10", () => { + const lastDigits = [4, 5, 6, 7, 8, 9, 10]; + const bases = [0, 10, 50, 90, 100]; + + for (const lastDigit of lastDigits) { + for (const base of bases) { + const finalNumber = base + lastDigit; + expect(getOrdinalNumber(finalNumber)).toEqual(`${finalNumber}th`); + } + } +}); + +// Case 8: Negative numbers +test("should return ordinal number for negative integer input", () => { + const ordinals = [ + { a: -2, b: "-2nd" }, + { a: -1, b: "-1st" }, + { a: -3, b: "-3rd" }, + { a: -4, b: "-4th" }, + { a: -11, b: "-11th" }, + { a: -12, b: "-12th" }, + { a: -13, b: "-13th" }, + ]; + + for (const { a, b } of ordinals) { + expect(getOrdinalNumber(a)).toEqual(b); + } +}); + +// Case 9: Decimal representations of integers +test("should return ordinal number for integer in decimal form", () => { + const ordinals = [ + { a: 1.0, b: "1st" }, + { a: 2.0, b: "2nd" }, + { a: 3.0, b: "3rd" }, + { a: 4.0, b: "4th" }, + { a: -11.0, b: "-11th" }, + { a: -12.0, b: "-12th" }, + { a: -13.0, b: "-13th" }, + ]; + + for (const { a, b } of ordinals) { + expect(getOrdinalNumber(a)).toEqual(b); + } +}); + +// Case 10: Invalid entries +test("should throw error for invalid inputs", () => { + const invalidEntries = [ + "one", + "", + [1], + null, + undefined, + 9.5, + 8.00001, + {}, + true, + ]; + + for (const entry of invalidEntries) { + expect(() => { + getOrdinalNumber(entry); + }).toThrow(); + } +}); From c5f8e47323fff06c6da4304d98b1be0aa8359dba Mon Sep 17 00:00:00 2001 From: Isaac Abodunrin Date: Thu, 19 Feb 2026 16:53:31 +0200 Subject: [PATCH 10/13] Implement get-ordinal-numbers --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 25 ++++++++++++++++++- 1 file changed, 24 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 f95d71db1..d69a4b98d 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,28 @@ function getOrdinalNumber(num) { - return "1st"; + if (typeof num != "number" || !Number.isInteger(num)) { + throw new Error("Please enter an integer"); + } + + const integerNumber = parseInt(num); + const positiveNumber = Math.abs(integerNumber); + + let suffix; + + switch (true) { + case positiveNumber % 10 == 1 && positiveNumber % 100 != 11: + suffix = "st"; + break; + case positiveNumber % 10 == 2 && positiveNumber % 100 != 12: + suffix = "nd"; + break; + case positiveNumber % 10 == 3 && positiveNumber % 100 != 13: + suffix = "rd"; + break; + default: + suffix = "th"; + } + + return `${integerNumber}${suffix}`; } module.exports = getOrdinalNumber; From 167a1ced8bb725142ebe65aa0abf2351a2d5cb43 Mon Sep 17 00:00:00 2001 From: Isaac Abodunrin Date: Thu, 19 Feb 2026 17:10:55 +0200 Subject: [PATCH 11/13] Remove dead code --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index d69a4b98d..97dd54a77 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -3,26 +3,25 @@ function getOrdinalNumber(num) { throw new Error("Please enter an integer"); } - const integerNumber = parseInt(num); - const positiveNumber = Math.abs(integerNumber); + const positiveNumber = Math.abs(num); let suffix; switch (true) { - case positiveNumber % 10 == 1 && positiveNumber % 100 != 11: + case positiveNumber % 10 === 1 && positiveNumber % 100 !== 11: suffix = "st"; break; - case positiveNumber % 10 == 2 && positiveNumber % 100 != 12: + case positiveNumber % 10 === 2 && positiveNumber % 100 !== 12: suffix = "nd"; break; - case positiveNumber % 10 == 3 && positiveNumber % 100 != 13: + case positiveNumber % 10 === 3 && positiveNumber % 100 !== 13: suffix = "rd"; break; default: suffix = "th"; } - return `${integerNumber}${suffix}`; + return `${num}${suffix}`; } module.exports = getOrdinalNumber; From d675e7323f359bb9c8c08168bcfc3b423d4334f8 Mon Sep 17 00:00:00 2001 From: Isaac Abodunrin Date: Fri, 20 Feb 2026 01:00:23 +0200 Subject: [PATCH 12/13] Testing for repeats --- Sprint-3/2-practice-tdd/repeat-str.test.js | 96 ++++++++++++++++------ 1 file changed, 71 insertions(+), 25 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index a3fc1196c..89c87bedd 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -1,32 +1,78 @@ -// 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. +// Case 1: handle count of 1 test("should repeat the string count times", () => { - const str = "hello"; - const count = 3; - const repeatedStr = repeatStr(str, count); - expect(repeatedStr).toEqual("hellohellohello"); + const repeats = [ + { a: "hello", b: 1, c: "hello" }, + { a: "Silo", b: 1, c: "Silo" }, + { a: "Ugly Betty", b: 1, c: "Ugly Betty" }, + { a: "ByteOil", b: 1, c: "ByteOil" }, + ]; + + for (const { a, b, c } of repeats) { + expect(repeatStr(a, b)).toEqual(c); + } +}); + +// Case 2: handle count greater than 1 +test("should return the original string repeated", () => { + const repeats = [ + { a: "hello", b: 3, c: "hellohellohello" }, + { a: "CYF ", b: 5, c: "CYF CYF CYF CYF CYF " }, + { + a: "My Year of Meat", + b: 2, + c: "My Year of MeatMy Year of Meat", + }, + { a: "Y2K", b: 10, c: "Y2KY2KY2KY2KY2KY2KY2KY2KY2KY2K" }, + ]; + + for (const { a, b, c } of repeats) { + expect(repeatStr(a, b)).toEqual(c); + } }); -// 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 3: Handle count of 0 +test("should return an empty string when count is 0", () => { + const repeats = [ + { a: "The Office", b: 0, c: "" }, + { a: "Angela Martin", b: 0, c: "" }, + { a: "Michael Scott", b: 0, c: "" }, + { a: "Kevin Malone", b: 0, c: "" }, + ]; -// 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. + for (const { a, b, c } of repeats) { + expect(repeatStr(a, b)).toEqual(c); + } +}); -// 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. +// Case 4: Handle negative count +test("should return an empty string when count is negative", () => { + const repeats = [ + { a: "Osuofia in London", b: -1 }, + { a: "Nkem Owoh", b: -5 }, + { a: "Mara Derwent", b: -10 }, + { a: "Cynthia Okereke", b: -100 }, + ]; + + for (const { a, b } of repeats) { + expect(() => repeatStr(a, b)).toThrow(); + } +}); + +// Case 5: Handle non-string input +test("should throw an error when input is not a string", () => { + const repeats = [ + { a: 123, b: 3 }, + { a: "123", b: "3" }, + { a: "true", b: true }, + { a: null, b: 1 }, + { a: "undefined", b: undefined }, + { a: {}, b: 4 }, + { a: [], b: 0 }, + ]; + + for (const { a, b } of repeats) { + expect(() => repeatStr(a, b)).toThrow(); + } +}); From 31a753f93deaac1a0568a97d88b317ca9d3ae572 Mon Sep 17 00:00:00 2001 From: Isaac Abodunrin Date: Fri, 20 Feb 2026 01:17:42 +0200 Subject: [PATCH 13/13] Implement repeat str function --- Sprint-3/2-practice-tdd/repeat-str.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 3838c7b00..e0ac371d0 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,5 +1,13 @@ -function repeatStr() { - return "hellohellohello"; +function repeatStr(str, count) { + if (typeof str !== "string") + throw new Error("Please use a string i.e. 'Hello World'"); + + if (!Number.isInteger(count)) + throw new Error("Please set count to an integer"); + + if (count < 0) throw new Error("Please use a count of 0 or greater"); + + return str.repeat(count); } module.exports = repeatStr;