From effcc4bc927175f13a8d01e3955b9d616be3de61 Mon Sep 17 00:00:00 2001 From: Khor Biel Date: Fri, 24 Oct 2025 11:56:11 +0100 Subject: [PATCH 1/3] Added and pass tests for countChar, getOrdinalNumber, and repeat functions --- Sprint-3/2-practice-tdd/count.js | 18 ++++++++- Sprint-3/2-practice-tdd/count.test.js | 6 +++ Sprint-3/2-practice-tdd/get-ordinal-number.js | 25 +++++++++++- .../2-practice-tdd/get-ordinal-number.test.js | 38 +++++++++++++++++++ Sprint-3/2-practice-tdd/repeat.js | 14 ++++++- Sprint-3/2-practice-tdd/repeat.test.js | 19 ++++++++++ 6 files changed, 116 insertions(+), 4 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..86ce7616c 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,21 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + // Ensure valid inputs + if ( + typeof stringOfCharacters !== "string" || + typeof findCharacter !== "string" + ) { + return 0; + } + + // Count occurrences + let count = 0; + for (let 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 42baf4b4b..ecb7e9ede 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 when the character is not found", () => { + const str = "hello"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db1..3b9362a82 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"; + // Ensure the input is a valid number + if (typeof num !== "number" || isNaN(num)) { + return ""; + } + + const lastTwoDigits = num % 100; + const lastDigit = num % 10; + + // Handle special cases: 11th, 12th, 13th + if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { + return `${num}th`; + } + + // Handle normal ordinal endings + switch (lastDigit) { + case 1: + return `${num}st`; + case 2: + return `${num}nd`; + case 3: + return `${num}rd`; + default: + return `${num}th`; + } } module.exports = getOrdinalNumber; 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..3eb2784a1 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -8,6 +8,44 @@ const getOrdinalNumber = require("./get-ordinal-number"); // When the number is 1, // Then the function should return "1st" + +// Case 1: Identify the ordinal number for 1 test("should return '1st' for 1", () => { expect(getOrdinalNumber(1)).toEqual("1st"); }); + +// Case 2: Identify the ordinal number for 2 +test("should return '2nd' for 2", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); +}); + +// Case 3: Identify the ordinal number for 3 +test("should return '3rd' for 3", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); +}); + +// Case 4: Identify the ordinal number for 4 +test("should return '4th' for 4", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); +}); + +// Case 5: Handle special cases 11, 12, 13 +test("should return '11th', '12th', '13th' for special cases", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); +}); + +// Case 6: Handle 21, 22, 23 correctly +test("should handle 21st, 22nd, 23rd correctly", () => { + expect(getOrdinalNumber(21)).toEqual("21st"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); +}); + +// Case 7: Handle numbers ending with 0, 4–9 +test("should return 'th' for numbers ending with 0, 4–9", () => { + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(14)).toEqual("14th"); + expect(getOrdinalNumber(19)).toEqual("19th"); +}); \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..3c90b7633 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,15 @@ -function repeat() { - return "hellohellohello"; +function repeat(str, count) { + // Validate inputs + if (typeof str !== "string") { + throw new Error("First argument must be a string"); + } + + if (typeof count !== "number" || count < 0) { + throw new Error("Count must be a non-negative number"); + } + + // Repeat string count times + return str.repeat(count); } module.exports = repeat; diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 34097b09c..258854407 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -20,13 +20,32 @@ 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 string when count is 1", () => { + const str = "hi"; + const count = 1; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("hi"); +}); // 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 when count is 0", () => { + const str = "test"; + 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 throw an error when count is negative", () => { + const str = "error"; + const count = -2; + expect(() => repeat(str, count)).toThrow( + "Count must be a non-negative number" + ); +}); From be7308ef629b1ef6ed4ed91e81eac7c9081f6f32 Mon Sep 17 00:00:00 2001 From: Khor Biel Date: Wed, 29 Oct 2025 10:40:49 +0000 Subject: [PATCH 2/3] Fixed some errors --- Sprint-3/2-practice-tdd/count.js | 22 +++------- Sprint-3/2-practice-tdd/get-ordinal-number.js | 40 +++++++++---------- Sprint-3/2-practice-tdd/repeat.js | 19 ++++----- Sprint-3/2-practice-tdd/repeat.test.js | 23 +++++------ 4 files changed, 45 insertions(+), 59 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 86ce7616c..c8111f435 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,21 +1,11 @@ function countChar(stringOfCharacters, findCharacter) { - // Ensure valid inputs - if ( - typeof stringOfCharacters !== "string" || - typeof findCharacter !== "string" - ) { - return 0; - } - - // Count occurrences - let count = 0; - for (let char of stringOfCharacters) { - if (char === findCharacter) { - count++; +let total = 0; + for (let i=0; i < stringOfCharacters.length; i++){ + if (findCharacter == stringOfCharacters[i]){ + total++; } - } - - return count; + } + return total; } module.exports = countChar; diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index 3b9362a82..a9e0ec9a9 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,28 +1,24 @@ function getOrdinalNumber(num) { - // Ensure the input is a valid number - if (typeof num !== "number" || isNaN(num)) { - return ""; - } - - const lastTwoDigits = num % 100; - const lastDigit = num % 10; + +let result; - // Handle special cases: 11th, 12th, 13th - if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { - return `${num}th`; + if (num % 100 == 11 || num % 100 == 12 || num % 100 == 13){ + result = num.toString() + "th"; } - - // Handle normal ordinal endings - switch (lastDigit) { - case 1: - return `${num}st`; - case 2: - return `${num}nd`; - case 3: - return `${num}rd`; - default: - return `${num}th`; + else if (num % 10 == 1){ + result = num.toString() +"st"; + } + else if (num % 10 == 2){ + result = num.toString() + "nd"; } -} + else if (num % 10 == 3){ + result = num.toString() + "rd"; + } + else { + result = num.toString() + "th"; + } + + return result +} module.exports = getOrdinalNumber; diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 3c90b7633..1e4af5184 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,15 +1,16 @@ function repeat(str, count) { - // Validate inputs - if (typeof str !== "string") { - throw new Error("First argument must be a string"); - } - if (typeof count !== "number" || count < 0) { - throw new Error("Count must be a non-negative number"); - } +if (count < 0) { + return null; +} else if (count == 0) { + return ""; +} +let result = ""; +for (let i = 0; i < count; i++) { + result += str; +} +return result; - // Repeat string count times - return str.repeat(count); } module.exports = repeat; diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 258854407..d4a8957ce 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -20,19 +20,19 @@ 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 string when count is 1", () => { - const str = "hi"; +test("handle Count of 1", () => { + const str = "hello"; const count = 1; const repeatedStr = repeat(str, count); - expect(repeatedStr).toEqual("hi"); + expect(repeatedStr).toEqual("hello"); }); // 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 when count is 0", () => { - const str = "test"; +test("handle Count of 0", () => { + const str = "hello"; const count = 0; const repeatedStr = repeat(str, count); expect(repeatedStr).toEqual(""); @@ -42,10 +42,9 @@ test("should return an empty string when count is 0", () => { // 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 throw an error when count is negative", () => { - const str = "error"; - const count = -2; - expect(() => repeat(str, count)).toThrow( - "Count must be a non-negative number" - ); -}); +test("Negative Count", () => { + const str = "hello"; + const count = -1; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual(null); +}); \ No newline at end of file From 18a4d3e9c5065e49cc38a9bb8cd6b54cedc1e682 Mon Sep 17 00:00:00 2001 From: Khor Biel Date: Fri, 31 Oct 2025 20:05:24 +0000 Subject: [PATCH 3/3] Fixed errors --- Sprint-3/2-practice-tdd/count.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index c8111f435..1459758d2 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,11 +1,11 @@ function countChar(stringOfCharacters, findCharacter) { -let total = 0; - for (let i=0; i < stringOfCharacters.length; i++){ - if (findCharacter == stringOfCharacters[i]){ - total++; + let count = 0; + for (let char of stringOfCharacters) { + if (char === findCharacter) { + count++; } - } - return total; + } + return count; } module.exports = countChar;