From 154190c8c9939b370bf25dbfcedb88c352a618af Mon Sep 17 00:00:00 2001 From: lintsang Date: Mon, 13 Jul 2026 19:38:12 +0100 Subject: [PATCH 1/2] 2-practice-tdd exercise --- Sprint-3/2-practice-tdd/count.js | 8 +++- Sprint-3/2-practice-tdd/count.test.js | 28 +++++++++++++ Sprint-3/2-practice-tdd/get-ordinal-number.js | 40 ++++++++++++++++++- .../2-practice-tdd/get-ordinal-number.test.js | 22 ++++++++++ Sprint-3/2-practice-tdd/repeat-str.js | 14 ++++++- Sprint-3/2-practice-tdd/repeat-str.test.js | 18 +++++++++ 6 files changed, 126 insertions(+), 4 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..dc867a33d4 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,11 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let countTime = 0; + for (let i = 0; i < stringOfCharacters.length; i++) { + if (stringOfCharacters.slice(i, i + 1) == findCharacter) {//take each character out from string to compare with the given character. If true, then add one to countTime. + countTime = countTime + 1; + } + } + return countTime; } module.exports = countChar; diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf7..4e2c31b065 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -17,8 +17,36 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(5); }); +test("should count multiple occurrences of a character", () => { + const str = "aaabcde"; + const char = "c"; + const count = countChar(str, char); + expect(count).toEqual(1); +}); + +test("should count multiple occurrences of a character", () => { + const str = "a2b3c4"; + const char = "3"; + const count = countChar(str, char); + expect(count).toEqual(1); +}); + +test("should count multiple occurrences of a character", () => { + const str = "4444444444444444444"; + const char = "4"; + const count = countChar(str, char); + expect(count).toEqual(19); +}); + // Scenario: No Occurrences // Given the input string `str`, // And a character `char` that does not exist within `str`. // When the function is called with these inputs, // Then it should return 0, indicating that no occurrences of `char` were found. + +test("should count no occurrence of a character", () => { + const str = "fghij"; + const char = "b"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db13..2cd59c6865 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,43 @@ function getOrdinalNumber(num) { - return "1st"; + let onesDigit; + + if (String(num).length > 1) { + //check if the number has more than 1 digit + onesDigit = Number(String(num).slice(-1)); //extract the figure at the tens digit, and convert it into a number. + const tensDigit = Number(String(num).slice(-2, -1)); //extract the figure at the tens digit, and convert it into a number. + + if (onesDigit == 1) { + if (tensDigit == 1) { + return num + "th"; + } else { + return num + "st"; + } + } else if (onesDigit == 2) { + if (tensDigit == 1) { + return num + "th"; + } + return num + "nd"; + } else if (onesDigit == 3) { + if (tensDigit == 1) { + return num + "th"; + } + return num + "rd"; + } else { + ("th"); + } + } else if (String(num).length == 1) { + onesDigit = Number(String(num)[0]); //extract the figure at the tens digit, and convert it into a number. + if (onesDigit == 1) { + return num + "st"; + console.log("Yeah youre in"); + } else if (onesDigit == 2) { + return num + "nd"; + } else if (onesDigit == 3) { + return num + "rd"; + } else { + 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 adfa58560f..59b3f219bd 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,25 @@ 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 '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"); +}); + +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 other numbers without ending with 1,2 or 3, except those ending with 1 in tens digit", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(113)).toEqual("113th"); + expect(getOrdinalNumber(213)).toEqual("213th"); + expect(getOrdinalNumber(313)).toEqual("313th"); + expect(getOrdinalNumber(1113)).toEqual("1113th"); +}); diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 2af0a2cea7..fd785b834c 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,7 +1,17 @@ -function repeatStr() { +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"; + let strOutput = ""; + if (count > 0){ + for (let i=0 ; i { // Given a target string `str` and a `count` equal to 1, // When the repeatStr function is called with these inputs, // Then it should return the original `str` without repetition. +test("should return the string with no reptition", () => { + const str = "morning"; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("morning"); +}); // Case: Handle count of 0: // Given a target string `str` and a `count` equal to 0, // When the repeatStr function is called with these inputs, // Then it should return an empty string. +test("should return empty string as the count is zero", () => { + const str = "afternoon"; + 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`, // When the repeatStr function is called with these inputs, // Then it should throw an error, as negative counts are not valid. +test("should return the string with no reptition", () => { + const str = "evening"; + const count = -1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("error"); +}); From 9dcdbd95fdba16309d6730cb96bb0215d5366494 Mon Sep 17 00:00:00 2001 From: lintsang Date: Fri, 24 Jul 2026 18:19:16 +0100 Subject: [PATCH 2/2] Revised all 2-practice-tdd exercises --- Sprint-3/2-practice-tdd/count.js | 2 +- Sprint-3/2-practice-tdd/count.test.js | 30 ++++++-------- Sprint-3/2-practice-tdd/get-ordinal-number.js | 41 +++++-------------- .../2-practice-tdd/get-ordinal-number.test.js | 21 ++++++++-- Sprint-3/2-practice-tdd/repeat-str.js | 6 +-- Sprint-3/2-practice-tdd/repeat-str.test.js | 11 +++-- 6 files changed, 47 insertions(+), 64 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index dc867a33d4..189f0a0cab 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,7 +1,7 @@ function countChar(stringOfCharacters, findCharacter) { let countTime = 0; for (let i = 0; i < stringOfCharacters.length; i++) { - if (stringOfCharacters.slice(i, i + 1) == findCharacter) {//take each character out from string to compare with the given character. If true, then add one to countTime. + if (stringOfCharacters[i] == findCharacter) {//take each character out from string to compare with the given character. If true, then add one to countTime. countTime = countTime + 1; } } diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 4e2c31b065..d0b62730ce 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -15,27 +15,21 @@ test("should count multiple occurrences of a character", () => { const char = "a"; const count = countChar(str, char); expect(count).toEqual(5); -}); -test("should count multiple occurrences of a character", () => { - const str = "aaabcde"; - const char = "c"; - const count = countChar(str, char); - expect(count).toEqual(1); -}); + const str2 = "4444444444444444444"; + const char2 = "4"; + const count2 = countChar(str2, char2); + expect(count2).toEqual(19); -test("should count multiple occurrences of a character", () => { - const str = "a2b3c4"; - const char = "3"; - const count = countChar(str, char); - expect(count).toEqual(1); -}); + const str3 = "aaabcde"; + const char3 = "c"; + const count3 = countChar(str3, char3); + expect(count3).toEqual(1); -test("should count multiple occurrences of a character", () => { - const str = "4444444444444444444"; - const char = "4"; - const count = countChar(str, char); - expect(count).toEqual(19); + const str4 = "a2b3c43"; + const char4 = "3"; + const count4 = countChar(str4, char4); + expect(count4).toEqual(2); }); // Scenario: No Occurrences diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index 2cd59c6865..5087efa0dc 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,38 +1,17 @@ function getOrdinalNumber(num) { - let onesDigit; - + let onesDigit = Number(String(num).slice(-1)); //extract the figure at the tens digit, and convert it into a number. + let tensDigit; if (String(num).length > 1) { - //check if the number has more than 1 digit - onesDigit = Number(String(num).slice(-1)); //extract the figure at the tens digit, and convert it into a number. - const tensDigit = Number(String(num).slice(-2, -1)); //extract the figure at the tens digit, and convert it into a number. - - if (onesDigit == 1) { - if (tensDigit == 1) { - return num + "th"; - } else { - return num + "st"; - } - } else if (onesDigit == 2) { - if (tensDigit == 1) { - return num + "th"; - } - return num + "nd"; - } else if (onesDigit == 3) { - if (tensDigit == 1) { - return num + "th"; - } - return num + "rd"; - } else { - ("th"); - } - } else if (String(num).length == 1) { - onesDigit = Number(String(num)[0]); //extract the figure at the tens digit, and convert it into a number. - if (onesDigit == 1) { + tensDigit = Number(String(num).slice(-2, -1)); //extract the figure at the tens digit, and convert it into a number. + } + if (tensDigit === 1) { + return num + "th"; + } else { + if (onesDigit === 1) { return num + "st"; - console.log("Yeah youre in"); - } else if (onesDigit == 2) { + } else if (onesDigit === 2) { return num + "nd"; - } else if (onesDigit == 3) { + } else if (onesDigit === 3) { return num + "rd"; } else { return num + "th"; 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 59b3f219bd..8be640ab69 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -13,25 +13,25 @@ const getOrdinalNumber = require("./get-ordinal-number"); // 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. -test("should append 'st' for numbers ending with 1, except those ending with 11", () => { +test("should append 'st' for numbers ending with 1, except those having figure 1 in tens digit", () => { expect(getOrdinalNumber(1)).toEqual("1st"); expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); }); -test("should append 'nd' for numbers ending with 2, except those ending with 12", () => { +test("should append 'nd' for numbers ending with 2, except those having figure 1 in tens digit", () => { expect(getOrdinalNumber(2)).toEqual("2nd"); expect(getOrdinalNumber(22)).toEqual("22nd"); expect(getOrdinalNumber(132)).toEqual("132nd"); }); -test("should append 'rd' for numbers ending with 3, except those ending with 13", () => { +test("should append 'rd' for numbers ending with 3, except those having figure 1 in tens digit", () => { expect(getOrdinalNumber(3)).toEqual("3rd"); expect(getOrdinalNumber(23)).toEqual("23rd"); expect(getOrdinalNumber(133)).toEqual("133rd"); }); -test("should append 'th' for other numbers without ending with 1,2 or 3, except those ending with 1 in tens digit", () => { +test("should append 'th' for other numbers ending with 1,2 or 3, except those having figure 1 in tens digit", () => { expect(getOrdinalNumber(11)).toEqual("11th"); expect(getOrdinalNumber(12)).toEqual("12th"); expect(getOrdinalNumber(13)).toEqual("13th"); @@ -40,3 +40,16 @@ test("should append 'th' for other numbers without ending with 1,2 or 3, except expect(getOrdinalNumber(313)).toEqual("313th"); expect(getOrdinalNumber(1113)).toEqual("1113th"); }); + +test("should append 'th' for other numbers ending other than 1,2 or 3", () => { + expect(getOrdinalNumber(5)).toEqual("5th"); + expect(getOrdinalNumber(8)).toEqual("8th"); + expect(getOrdinalNumber(9)).toEqual("9th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(57)).toEqual("57th"); + expect(getOrdinalNumber(79)).toEqual("79th"); + expect(getOrdinalNumber(94)).toEqual("94th"); + expect(getOrdinalNumber(266)).toEqual("266th"); + expect(getOrdinalNumber(465)).toEqual("465th"); + expect(getOrdinalNumber(1070)).toEqual("1070th"); +}); diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index fd785b834c..30842ed4f5 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -2,15 +2,13 @@ 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. let strOutput = ""; - if (count > 0){ + if (count >= 0){ for (let i=0 ; i { // 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 return the string with no reptition", () => { - const str = "evening"; - const count = -1; - const repeatedStr = repeatStr(str, count); - expect(repeatedStr).toEqual("error"); -}); +test("should throw an error as a negative count is not valid", () => { + expect(() => { + repeatStr("evening", -1); + }).toThrow(); +}); \ No newline at end of file