From 2c2b0f39b7e5fad09572d7c26edf7dc04c7f4167 Mon Sep 17 00:00:00 2001 From: iswat Date: Thu, 23 Oct 2025 19:17:30 +0100 Subject: [PATCH 01/25] Add test for countChar when character occurs zero times --- Sprint-3/2-practice-tdd/count.test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 42baf4b4b..b670c0c61 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,9 @@ test("should count multiple occurrences of a character", () => { // 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("should return 0 occurence of a character", () => { + const str = "happy"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}) \ No newline at end of file From 6db0981f538e96a070e02e48e61683b4eb56d7b6 Mon Sep 17 00:00:00 2001 From: iswat Date: Thu, 23 Oct 2025 20:05:18 +0100 Subject: [PATCH 02/25] Implement countChar function to count occurrences of a character in a string --- Sprint-3/2-practice-tdd/count.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..f9e79f38b 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,6 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + const characterOccurrence = stringOfCharacters.split(findCharacter).length - 1; + return characterOccurrence; } module.exports = countChar; From 7362e8698bb40651c628becb2852f02da112e58f Mon Sep 17 00:00:00 2001 From: iswat Date: Thu, 23 Oct 2025 21:54:57 +0100 Subject: [PATCH 03/25] Add test cases using jest for function getOrdinalNumber --- Sprint-3/2-practice-tdd/get-ordinal-number.test.js | 12 ++++++++++++ 1 file changed, 12 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 dfe4b6091..8428e9a41 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,15 @@ const getOrdinalNumber = require("./get-ordinal-number"); test("should return '1st' for 1", () => { expect(getOrdinalNumber(1)).toEqual("1st"); }); + +test("should return '11th' for 11", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); +}) + +test("should return '21st' for 21", () => { + expect(getOrdinalNumber(21)).toEqual("21st"); +}) + +test("should return '2nd' for 2", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); +}) From 1f72b490505ec927abc3a04ac5616d488d838b01 Mon Sep 17 00:00:00 2001 From: iswat Date: Thu, 23 Oct 2025 22:04:39 +0100 Subject: [PATCH 04/25] Add the implementation for the function getOrdinalNumber --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 9 ++++++++- 1 file changed, 8 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..a74cffdb2 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,12 @@ function getOrdinalNumber(num) { - return "1st"; + if (num === 1 || num === 21) { + return `${num}st`; + } else if (num === 11) { + return `${num}th`; + } else if (num === 2) { + return `${num}nd`; + } } + module.exports = getOrdinalNumber; From ed1a3939262e476777771aa6619f2209b9b2cd35 Mon Sep 17 00:00:00 2001 From: iswat Date: Thu, 23 Oct 2025 22:22:44 +0100 Subject: [PATCH 05/25] Add test cases using jest for the function repeat --- Sprint-3/2-practice-tdd/repeat.test.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 34097b09c..5b239f5cc 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -20,13 +20,31 @@ test("should repeat the string count times", () => { // 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("should return the original input with no repetition", () => { + const str = "milk"; + const count = 1; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual(str); +}) // 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("should return an empty string", () => { + const str = "rice"; + 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("should return an error message", () => { + const str = "food"; + const count = -2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("Negative number invalid"); +}) From 93d9cb6604c2689694ea400e84f589d06373822c Mon Sep 17 00:00:00 2001 From: iswat Date: Thu, 23 Oct 2025 22:47:05 +0100 Subject: [PATCH 06/25] Add implementation for the function repeat --- Sprint-3/2-practice-tdd/repeat.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..cfa2ffdd4 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(valueToRepeat, numOfTimes) { + let repeatedValue = ""; + if (numOfTimes > 0) { + for (let i = 0; i < numOfTimes; i++) { + repeatedValue += valueToRepeat; + } + } else if (numOfTimes === 0) { + repeatedValue = ""; + } + else { + repeatedValue = "Negative number invalid"; + } + return repeatedValue; } module.exports = repeat; From 9bc1c34a2f204106cb45c77cce74dca5383a900e Mon Sep 17 00:00:00 2001 From: iswat Date: Mon, 27 Oct 2025 20:14:06 +0000 Subject: [PATCH 07/25] Add test to verify countChar returns 1 when the character appears once --- Sprint-3/2-practice-tdd/count.test.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index b670c0c61..73b77089b 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -27,4 +27,17 @@ test("should return 0 occurence of a character", () => { const char = "z"; const count = countChar(str, char); expect(count).toEqual(0); -}) \ No newline at end of file +}); + +// Scenario: One occurrence +// Given an input string `str`, +// And a character `char` that occurs once within `str` (e.g., 'a' in 'boat'), +// When the function is called with these inputs, +// Then it should correctly return the count of `char` (e.g., 'a' appears once in 'boat'). + +test("should return 1 occurence of a character", () => { + const str = "boat"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(1); +}); From 01cb97808c811afec5ba8fad2c46a4464d456531 Mon Sep 17 00:00:00 2001 From: iswat Date: Mon, 27 Oct 2025 20:52:09 +0000 Subject: [PATCH 08/25] Add unit test to verify countChar returns 0 when given an empty string --- 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 73b77089b..94734a68b 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -41,3 +41,11 @@ test("should return 1 occurence of a character", () => { const count = countChar(str, char); expect(count).toEqual(1); }); + +test("should return 0 occurrence of character in an empty string", () => { + const str = ""; + const char = "b"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + From 86311c728d25d96de665b25349ad07c193252b07 Mon Sep 17 00:00:00 2001 From: iswat Date: Mon, 27 Oct 2025 21:54:22 +0000 Subject: [PATCH 09/25] Add a test case for multi-character input in countChar Add a test to ensure countChar throws an error when more than one character is passed as the 'char' argument, returning a descriptive invalid input message. --- 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 94734a68b..1390301d6 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -49,3 +49,11 @@ test("should return 0 occurrence of character in an empty string", () => { expect(count).toEqual(0); }); +test("should throw an error when more than one string is passed as char", () => { + const str = "jump"; + const char = "pp"; + const count = countChar(str, char); + expect(count).toEqual("invalid input: Input just one character"); +}); + + From bd7847fb2d07f922f8d1ce67c144d489d5bdba75 Mon Sep 17 00:00:00 2001 From: iswat Date: Mon, 27 Oct 2025 22:39:46 +0000 Subject: [PATCH 10/25] Add test cases for countChar to check expected outputs in various scenarios --- Sprint-3/2-practice-tdd/count.test.js | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 1390301d6..b191f4ce9 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -56,4 +56,37 @@ test("should throw an error when more than one string is passed as char", () => expect(count).toEqual("invalid input: Input just one character"); }); +test("should count characters regardless of case (treat 'a' and 'A' as equal)", () => { + const str = "JUMP"; + const char = "m"; + const count = countChar(str, char); + expect(count).toEqual(1); +}); + +test("should return correct count of space characters", () => { + const str = "F r o NT"; + const char = " "; + const count = countChar(str, char); + expect(count).toEqual(3); +}); +test("should count numeric characters correctly in the string", () => { + const str = "2Languages6"; + const char = "2"; + const count = countChar(str, char); + expect(count).toEqual(1); +}); + +test("should correctly count characters in a very long string", () => { + const str = "b".repeat(500) + "abB"; + const char = "b"; + const count = countChar(str, char); + expect(count).toEqual(502); +}); + +test("should count numeric characters correctly in the string", () => { + const str = "2Languages6"; + const char = 2; + const count = countChar(str, char); + expect(count).toEqual("Invalid input: input should be a string"); +}); \ No newline at end of file From 1c66099595196c801e244fdaffd33802942afc85 Mon Sep 17 00:00:00 2001 From: iswat Date: Mon, 27 Oct 2025 22:43:11 +0000 Subject: [PATCH 11/25] Update countChar implementation to correctly count character occurrences --- Sprint-3/2-practice-tdd/count.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index f9e79f38b..e8b116b3c 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,17 @@ function countChar(stringOfCharacters, findCharacter) { - const characterOccurrence = stringOfCharacters.split(findCharacter).length - 1; + let characterOccurrence = ""; + if (typeof stringOfCharacters === "string" && typeof findCharacter === "string") { + stringOfCharacters = stringOfCharacters.toLowerCase(); + findCharacter = findCharacter.toLowerCase(); + if (findCharacter.length === 1) { + characterOccurrence = stringOfCharacters.split(findCharacter).length - 1; + } else { + characterOccurrence = "invalid input: Input just one character"; + } + } else { + characterOccurrence = "Invalid input: input should be a string"; + } + return characterOccurrence; } From 03b238e8b7384bed86087bcda21ab1447a892cfb Mon Sep 17 00:00:00 2001 From: iswat Date: Mon, 27 Oct 2025 22:50:37 +0000 Subject: [PATCH 12/25] Fix indentation in count.js and count.test.js --- Sprint-3/2-practice-tdd/count.js | 5 ++++- Sprint-3/2-practice-tdd/count.test.js | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index e8b116b3c..c2d88cd84 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,6 +1,9 @@ function countChar(stringOfCharacters, findCharacter) { let characterOccurrence = ""; - if (typeof stringOfCharacters === "string" && typeof findCharacter === "string") { + if ( + typeof stringOfCharacters === "string" && + typeof findCharacter === "string" + ) { stringOfCharacters = stringOfCharacters.toLowerCase(); findCharacter = findCharacter.toLowerCase(); if (findCharacter.length === 1) { diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index b191f4ce9..69e5fae75 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -89,4 +89,4 @@ test("should count numeric characters correctly in the string", () => { const char = 2; const count = countChar(str, char); expect(count).toEqual("Invalid input: input should be a string"); -}); \ No newline at end of file +}); From 14306cc73c9b4cc3bb1904df792490f3c806654f Mon Sep 17 00:00:00 2001 From: iswat Date: Mon, 27 Oct 2025 23:37:29 +0000 Subject: [PATCH 13/25] Add test cases to ensure correct ordinal output for 12, and 13 --- .../2-practice-tdd/get-ordinal-number.test.js | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 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 8428e9a41..667445789 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -12,14 +12,40 @@ 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 '10th' for 10", () => { + expect(getOrdinalNumber(10)).toEqual("10th"); +}); + 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"); +}); + test("should return '21st' for 21", () => { expect(getOrdinalNumber(21)).toEqual("21st"); }) -test("should return '2nd' for 2", () => { - expect(getOrdinalNumber(2)).toEqual("2nd"); -}) + + +test("should return '111th' for 111", () => { + expect(getOrdinalNumber(111)).toEqual("111th"); +}); \ No newline at end of file From a6bbe867c4eba127c412b83b6dd5e4ddc9fef938 Mon Sep 17 00:00:00 2001 From: iswat Date: Tue, 28 Oct 2025 10:48:00 +0000 Subject: [PATCH 14/25] Fix getOrdinalNumber to handle integers correctly and reject invalid inputs --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index a74cffdb2..03c0a6550 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,12 +1,31 @@ function getOrdinalNumber(num) { - if (num === 1 || num === 21) { - return `${num}st`; - } else if (num === 11) { - return `${num}th`; - } else if (num === 2) { - return `${num}nd`; + let ordinalNum; + if (Number.isInteger(num) === true) { + if ( + Math.abs(num % 100) === 11 || + Math.abs(num % 100) === 12 || + Math.abs(num % 100) === 13 || + Math.abs(num % 10) === 4 || + Math.abs(num % 10) === 5 || + Math.abs(num % 10) === 6 || + Math.abs(num % 10) === 7 || + Math.abs(num % 10) === 8 || + Math.abs(num % 10) === 9 || + Math.abs(num % 10) === 0 + ) { + ordinalNum = `${num}th`; + } else if (Math.abs(num % 10) === 1) { + ordinalNum = `${num}st`; + } else if (Math.abs(num % 10) === 2) { + ordinalNum = `${num}nd`; + } else if (Math.abs(num % 10) === 3) { + ordinalNum = `${num}rd`; + } else { + ordinalNum = `${num}th`; + } + } else { + ordinalNum = "Invalid input: Input is an integer"; } + return ordinalNum; } - - module.exports = getOrdinalNumber; From aaa3da4c1aa9dd39d662c71cdd7f18283431c612 Mon Sep 17 00:00:00 2001 From: iswat Date: Tue, 28 Oct 2025 10:53:28 +0000 Subject: [PATCH 15/25] Add integer, negative, zero, and invalid input test cases for getOrdinalNumber --- .../2-practice-tdd/get-ordinal-number.test.js | 46 +++++++++++++++++-- 1 file changed, 43 insertions(+), 3 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 667445789..efb242ef5 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -30,7 +30,7 @@ test("should return '10th' for 10", () => { test("should return '11th' for 11", () => { expect(getOrdinalNumber(11)).toEqual("11th"); -}) +}); test("should return '12th' for 12", () => { expect(getOrdinalNumber(12)).toEqual("12th"); @@ -42,10 +42,50 @@ test("should return '13th' for 13", () => { test("should return '21st' for 21", () => { expect(getOrdinalNumber(21)).toEqual("21st"); -}) +}); + +test("should return '23rd' for 23", () => { + expect(getOrdinalNumber(23)).toEqual("23rd"); +}); +test("should return '22nd' for 22", () => { + expect(getOrdinalNumber(22)).toEqual("22nd"); +}); +test("should return '100th' for 100", () => { + expect(getOrdinalNumber(100)).toEqual("100th"); +}); test("should return '111th' for 111", () => { expect(getOrdinalNumber(111)).toEqual("111th"); -}); \ No newline at end of file +}); + +test("should return '0th' for 0", () => { + expect(getOrdinalNumber(0)).toEqual("0th"); +}); + +test("should return '-111th' for -111", () => { + expect(getOrdinalNumber(-111)).toEqual("-111th"); +}); + +test("should return 'Invalid input: Input is an integer' for 1.52", () => { + expect(getOrdinalNumber(1.52)).toEqual("Invalid input: Input is an integer"); +}); + +test("should return 'Invalid input: Input is an integer' for null", () => { + expect(getOrdinalNumber(null)).toEqual("Invalid input: Input is an integer"); +}); + +test("should return 'Invalid input: Input is an integer' for undefined", () => { + expect(getOrdinalNumber(undefined)).toEqual( + "Invalid input: Input is an integer" + ); +}); + +test("should return 'Invalid input: Input is an integer' for 'a'", () => { + expect(getOrdinalNumber("a")).toEqual("Invalid input: Input is an integer"); +}); + +test("should return 'Invalid input: Input is an integer' for {}", () => { + expect(getOrdinalNumber({})).toEqual("Invalid input: Input is an integer"); +}); From 4548cb54450efb445ebe7ba89cb1382220ad003c Mon Sep 17 00:00:00 2001 From: iswat Date: Tue, 28 Oct 2025 12:17:02 +0000 Subject: [PATCH 16/25] Update repeat function to handle positive, zero, negative, non-integer counts and repeat strings, numbers, booleans, null, undefined, arrays, and objects --- Sprint-3/2-practice-tdd/repeat.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index cfa2ffdd4..1d126e4ac 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,14 +1,15 @@ function repeat(valueToRepeat, numOfTimes) { let repeatedValue = ""; - if (numOfTimes > 0) { + if (numOfTimes > 0 && Number.isInteger(numOfTimes)) { for (let i = 0; i < numOfTimes; i++) { repeatedValue += valueToRepeat; } } else if (numOfTimes === 0) { repeatedValue = ""; - } - else { + } else if(numOfTimes < 0) { repeatedValue = "Negative number invalid"; + } else if(!Number.isInteger(numOfTimes)) { + repeatedValue = "Invalid count: count should be an integer" } return repeatedValue; } From 4027919e1773a31bb04bbbc89ec46a6a6ea81018 Mon Sep 17 00:00:00 2001 From: iswat Date: Tue, 28 Oct 2025 12:20:07 +0000 Subject: [PATCH 17/25] Add test cases for repeat function to cover edge cases and various input types --- Sprint-3/2-practice-tdd/repeat.test.js | 69 +++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 5b239f5cc..a9e8447b3 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -31,7 +31,7 @@ test("should return the original input with no repetition", () => { // 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("should return an empty string", () => { +test("should return an empty string for zero count times", () => { const str = "rice"; const count = 0; const repeatedStr = repeat(str, count); @@ -48,3 +48,70 @@ test("should return an error message", () => { const repeatedStr = repeat(str, count); expect(repeatedStr).toEqual("Negative number invalid"); }) + +test("should return an empty string when empty string is expected count number of times", () => { + const str = ""; + const count = 23; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual(""); +}) + +test("should repeat the number count times as a string", () => { + const str = 1; + const count = 3; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("111"); +}); + +test("should repeat boolean count times as a string", () => { + const str = true; + const count = 2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("truetrue"); +}); + +test("should repeat null count times as a string", () => { + const str = null; + const count = 2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("nullnull"); +}); + +test("should repeat undefined count times as a string", () => { + const str = undefined; + const count = 2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("undefinedundefined"); +}); + +// case: array input +test("should repeat [] count times as a string", () => { + const str = []; + const count = 2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual(""); +}); + +// case: Non-integer positive count +test("should return an error message for non-integer positive count", () => { + const str = "apple"; + const count = 2.5; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("Invalid count: count should be an integer"); +}); + +// case: Non-integer negative count +test("should return an error message for non-integer negative count", () => { + const str = "banana"; + const count = -1.7; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("Negative number invalid"); +}); + +// case: Object input +test("should repeat an object count times as a string", () => { + const str = {}; + const count = 2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("[object Object][object Object]"); +}); From 2b82934071bbf5aec0c0bd50fbdbb997da9d6a76 Mon Sep 17 00:00:00 2001 From: iswat Date: Wed, 29 Oct 2025 23:37:53 +0000 Subject: [PATCH 18/25] Refactor countChar function to return early for invalid inputs and remove redundant code --- Sprint-3/2-practice-tdd/count.js | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index c2d88cd84..c3b27cd1b 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,21 +1,23 @@ function countChar(stringOfCharacters, findCharacter) { - let characterOccurrence = ""; + // Check both inputs are strings if ( - typeof stringOfCharacters === "string" && - typeof findCharacter === "string" + typeof stringOfCharacters !== "string" || + typeof findCharacter !== "string" ) { - stringOfCharacters = stringOfCharacters.toLowerCase(); - findCharacter = findCharacter.toLowerCase(); - if (findCharacter.length === 1) { - characterOccurrence = stringOfCharacters.split(findCharacter).length - 1; - } else { - characterOccurrence = "invalid input: Input just one character"; - } - } else { - characterOccurrence = "Invalid input: input should be a string"; + return "Invalid input: input should be a string"; } - return characterOccurrence; + // Convert both to lowercase for case-insensitive matching + const str = stringOfCharacters.toLowerCase(); + const char = findCharacter.toLowerCase(); + + // Check that only one character is passed + if (char.length !== 1) { + return "invalid input: Input just one character"; + } + + // Return count (0 for empty strings works naturally) + return str.split(char).length - 1; } module.exports = countChar; From 415363288b00a26706145d44d34697a7157bfc88 Mon Sep 17 00:00:00 2001 From: iswat Date: Wed, 29 Oct 2025 23:47:57 +0000 Subject: [PATCH 19/25] Refactor getOrdinalNumber function to return early for invalid input and remove redundant code --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 48 ++++++++----------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index 03c0a6550..f6a54df69 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,31 +1,25 @@ function getOrdinalNumber(num) { - let ordinalNum; - if (Number.isInteger(num) === true) { - if ( - Math.abs(num % 100) === 11 || - Math.abs(num % 100) === 12 || - Math.abs(num % 100) === 13 || - Math.abs(num % 10) === 4 || - Math.abs(num % 10) === 5 || - Math.abs(num % 10) === 6 || - Math.abs(num % 10) === 7 || - Math.abs(num % 10) === 8 || - Math.abs(num % 10) === 9 || - Math.abs(num % 10) === 0 - ) { - ordinalNum = `${num}th`; - } else if (Math.abs(num % 10) === 1) { - ordinalNum = `${num}st`; - } else if (Math.abs(num % 10) === 2) { - ordinalNum = `${num}nd`; - } else if (Math.abs(num % 10) === 3) { - ordinalNum = `${num}rd`; - } else { - ordinalNum = `${num}th`; - } - } else { - ordinalNum = "Invalid input: Input is an integer"; + // Early return for invalid inputs + if (!Number.isInteger(num)) { + return "Invalid input: Input is an integer"; } - return ordinalNum; + + const absNum = Math.abs(num); + const lastTwo = absNum % 100; + const lastDigit = absNum % 10; + + // Handle special cases: 11th, 12th, 13th + if (lastTwo === 11 || lastTwo === 12 || lastTwo === 13) { + return `${num}th`; + } + + // Handle regular suffixes + if (lastDigit === 1) return `${num}st`; + if (lastDigit === 2) return `${num}nd`; + if (lastDigit === 3) return `${num}rd`; + + // Default suffix + return `${num}th`; } + module.exports = getOrdinalNumber; From 2957d4b8d0d92e8cb8e3b9dce723b497d3ae416e Mon Sep 17 00:00:00 2001 From: iswat Date: Thu, 30 Oct 2025 00:00:22 +0000 Subject: [PATCH 20/25] Fix inconsitent icode indentation --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 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 f6a54df69..f39df5b98 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -14,10 +14,17 @@ function getOrdinalNumber(num) { } // Handle regular suffixes - if (lastDigit === 1) return `${num}st`; - if (lastDigit === 2) return `${num}nd`; - if (lastDigit === 3) return `${num}rd`; + if (lastDigit === 1) { + return `${num}st`; + } + + if (lastDigit === 2) { + return `${num}nd`; + } + if (lastDigit === 3) { + return `${num}rd`; + } // Default suffix return `${num}th`; } From 95ba4bf0dd6a279ae010b80da2c34aace646d9c7 Mon Sep 17 00:00:00 2001 From: iswat Date: Thu, 30 Oct 2025 00:07:10 +0000 Subject: [PATCH 21/25] Refactor repeat function to return early for invalid input and remove redundant code --- Sprint-3/2-practice-tdd/repeat.js | 33 +++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 1d126e4ac..0c053f93a 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,16 +1,29 @@ function repeat(valueToRepeat, numOfTimes) { + // Validate count + if (!Number.isInteger(numOfTimes)) { + return "Invalid count: count should be an integer"; + } + if (numOfTimes < 0) { + return "Negative number invalid"; + } + if (numOfTimes === 0) { + return ""; + } + + // Convert arrays to empty string + if (Array.isArray(valueToRepeat)) { + valueToRepeat = ""; + } + + // Convert other types to string + const strValue = String(valueToRepeat); + + // Repeat the string let repeatedValue = ""; - if (numOfTimes > 0 && Number.isInteger(numOfTimes)) { - for (let i = 0; i < numOfTimes; i++) { - repeatedValue += valueToRepeat; - } - } else if (numOfTimes === 0) { - repeatedValue = ""; - } else if(numOfTimes < 0) { - repeatedValue = "Negative number invalid"; - } else if(!Number.isInteger(numOfTimes)) { - repeatedValue = "Invalid count: count should be an integer" + for (let i = 0; i < numOfTimes; i++) { + repeatedValue += strValue; } + return repeatedValue; } From d0b63785c0658094835b02f560847306afe9d4b3 Mon Sep 17 00:00:00 2001 From: iswat Date: Thu, 30 Oct 2025 10:10:35 +0000 Subject: [PATCH 22/25] Update expected output for negative float to 'Invalid count: count should be an integer' in Jest test case --- Sprint-3/2-practice-tdd/repeat.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index a9e8447b3..e4ce60f0e 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -105,7 +105,7 @@ test("should return an error message for non-integer negative count", () => { const str = "banana"; const count = -1.7; const repeatedStr = repeat(str, count); - expect(repeatedStr).toEqual("Negative number invalid"); + expect(repeatedStr).toEqual("Invalid count: count should be an integer"); }); // case: Object input From 32437489f79fc6301fde32daa2f5ae79a4531745 Mon Sep 17 00:00:00 2001 From: iswat Date: Thu, 30 Oct 2025 11:25:46 +0000 Subject: [PATCH 23/25] Update error message for numOfTimes validation to "Invalid numOfTimes: numOfTimes should be an integer" --- Sprint-3/2-practice-tdd/repeat.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 0c053f93a..6515475dd 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,7 +1,7 @@ function repeat(valueToRepeat, numOfTimes) { - // Validate count + // Validate numOfTimes if (!Number.isInteger(numOfTimes)) { - return "Invalid count: count should be an integer"; + return "Invalid numOfTimes: numOfTimes should be an integer"; } if (numOfTimes < 0) { return "Negative number invalid"; From adb94216ba51e99dbb829b87d0046d25d3f41921 Mon Sep 17 00:00:00 2001 From: iswat Date: Thu, 30 Oct 2025 13:18:07 +0000 Subject: [PATCH 24/25] Fix function to allow only string for valueToRepeat and positive integer for numOfTimes; all other inputs return an error message --- Sprint-3/2-practice-tdd/repeat.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 6515475dd..af477773c 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,8 +1,13 @@ function repeat(valueToRepeat, numOfTimes) { + // Validate valueToRepeat + if (typeof valueToRepeat !== "string") { + return "Invalid input: valueToRepeat should be a string"; + } // Validate numOfTimes if (!Number.isInteger(numOfTimes)) { - return "Invalid numOfTimes: numOfTimes should be an integer"; + return "Invalid input: numOfTimes should be an integer"; } + if (numOfTimes < 0) { return "Negative number invalid"; } @@ -10,18 +15,11 @@ function repeat(valueToRepeat, numOfTimes) { return ""; } - // Convert arrays to empty string - if (Array.isArray(valueToRepeat)) { - valueToRepeat = ""; - } - - // Convert other types to string - const strValue = String(valueToRepeat); // Repeat the string let repeatedValue = ""; for (let i = 0; i < numOfTimes; i++) { - repeatedValue += strValue; + repeatedValue += valueToRepeat; } return repeatedValue; From f6115befbc9309ee184820021e5a82403c4540c6 Mon Sep 17 00:00:00 2001 From: iswat Date: Thu, 30 Oct 2025 13:20:19 +0000 Subject: [PATCH 25/25] Update test descriptions and expected output values in test cases --- Sprint-3/2-practice-tdd/repeat.test.js | 68 +++++++++++++++++++------- 1 file changed, 49 insertions(+), 19 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index e4ce60f0e..5428d4f39 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -25,7 +25,7 @@ test("should return the original input with no repetition", () => { const count = 1; const repeatedStr = repeat(str, count); expect(repeatedStr).toEqual(str); -}) +}); // case: Handle Count of 0: // Given a target string str and a count equal to 0, @@ -36,60 +36,70 @@ test("should return an empty string for zero count times", () => { 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("should return an error message", () => { +test("should return an error message when negative number count is passed", () => { const str = "food"; const count = -2; const repeatedStr = repeat(str, count); expect(repeatedStr).toEqual("Negative number invalid"); -}) +}); test("should return an empty string when empty string is expected count number of times", () => { const str = ""; const count = 23; const repeatedStr = repeat(str, count); expect(repeatedStr).toEqual(""); -}) +}); -test("should repeat the number count times as a string", () => { +test("should return an error message when a number is passed as input", () => { const str = 1; const count = 3; const repeatedStr = repeat(str, count); - expect(repeatedStr).toEqual("111"); + expect(repeatedStr).toEqual( + "Invalid input: valueToRepeat should be a string" + ); }); -test("should repeat boolean count times as a string", () => { +test("should return an error message when 'true' is passed as input", () => { const str = true; const count = 2; const repeatedStr = repeat(str, count); - expect(repeatedStr).toEqual("truetrue"); + expect(repeatedStr).toEqual( + "Invalid input: valueToRepeat should be a string" + ); }); -test("should repeat null count times as a string", () => { +test("should return an error message for 'null' as input", () => { const str = null; const count = 2; const repeatedStr = repeat(str, count); - expect(repeatedStr).toEqual("nullnull"); + expect(repeatedStr).toEqual( + "Invalid input: valueToRepeat should be a string" + ); }); -test("should repeat undefined count times as a string", () => { +test("should return an error message for 'undefined' as input", () => { const str = undefined; const count = 2; const repeatedStr = repeat(str, count); - expect(repeatedStr).toEqual("undefinedundefined"); + expect(repeatedStr).toEqual( + "Invalid input: valueToRepeat should be a string" + ); }); // case: array input -test("should repeat [] count times as a string", () => { +test("should return an error message for 'array' as input", () => { const str = []; const count = 2; const repeatedStr = repeat(str, count); - expect(repeatedStr).toEqual(""); + expect(repeatedStr).toEqual( + "Invalid input: valueToRepeat should be a string" + ); }); // case: Non-integer positive count @@ -97,7 +107,7 @@ test("should return an error message for non-integer positive count", () => { const str = "apple"; const count = 2.5; const repeatedStr = repeat(str, count); - expect(repeatedStr).toEqual("Invalid count: count should be an integer"); + expect(repeatedStr).toEqual("Invalid input: numOfTimes should be an integer"); }); // case: Non-integer negative count @@ -105,13 +115,33 @@ test("should return an error message for non-integer negative count", () => { const str = "banana"; const count = -1.7; const repeatedStr = repeat(str, count); - expect(repeatedStr).toEqual("Invalid count: count should be an integer"); + expect(repeatedStr).toEqual("Invalid input: numOfTimes should be an integer"); }); // case: Object input -test("should repeat an object count times as a string", () => { +test("should return an error message when object is passed as input", () => { const str = {}; const count = 2; const repeatedStr = repeat(str, count); - expect(repeatedStr).toEqual("[object Object][object Object]"); + expect(repeatedStr).toEqual( + "Invalid input: valueToRepeat should be a string" + ); +}); + +test("should return an error message when input is not a string and count is not a number", () => { + const str = {}; + const count = -2.2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual( + "Invalid input: valueToRepeat should be a string" + ); +}); + +test("should return an error message when input is not a string and count is not a number", () => { + const str = {}; + const count = -2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual( + "Invalid input: valueToRepeat should be a string" + ); });