From 3adb2de6888a0c3960c81dc4dbdace7f0c94d0cb Mon Sep 17 00:00:00 2001 From: Rizqah Date: Sat, 11 Jul 2026 19:09:01 +0100 Subject: [PATCH 1/2] count and get ordinal number implemented and tests written --- Sprint-3/2-practice-tdd/count.js | 2 +- Sprint-3/2-practice-tdd/count.test.js | 25 ++++++++++++++++++ Sprint-3/2-practice-tdd/get-ordinal-number.js | 16 +++++++++++- .../2-practice-tdd/get-ordinal-number.test.js | 26 +++++++++++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..b4ba688179 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,5 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + return stringOfCharacters.split(findCharacter).length-1 } 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..e38d27a33c 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -16,9 +16,34 @@ test("should count multiple occurrences of a character", () => { const count = countChar(str, char); expect(count).toEqual(5); }); +test("should count multiple occurrences of a character", () => { + const str = "pneumonoultramicroscopicsilicovolcanoconoisis"; + const char = "o"; + const count = countChar(str, char); + expect(count).toEqual(9); +}); +test("should count multiple occurrences of a character", () => { + const str = "pneumonoultramicroscopicsilicovolcanoconoisis"; + const char = "i"; + const count = countChar(str, char); + expect(count).toEqual(6); +}); + // 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 multiple occurrences of a character", () => { + const str = "interesting"; + const char = "b"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); +test("should count multiple occurrences of a character", () => { + const str = "future"; + const char = "m"; + 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 f95d71db13..b61de41384 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,19 @@ function getOrdinalNumber(num) { - return "1st"; + if ([11, 12, 13].includes(num % 100)){ + return num + "th" + } + if (String(num).at(-1)=== "1"){ + return num + "st" + } + if (String(num).at(-1) === "2"){ + return num + "nd" + } + if (String(num).at(-1) === "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..64a23f7594 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -17,4 +17,30 @@ 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"); + expect(getOrdinalNumber(1021)).toEqual("1021st"); }); +//case 2; Numbers ending with 4,5,6,7,8,9 and 0 +test("should append 'th' for numbers ending with 4,5,6,7,8,9,0", () => { + expect(getOrdinalNumber(104)).toEqual("104th"); + expect(getOrdinalNumber(2000)).toEqual("2000th"); + expect(getOrdinalNumber(167)).toEqual("167th"); + expect(getOrdinalNumber(19)).toEqual("19th"); +}); +//case 3; Numbers ending with 11,12,13 +test("should append 'th' for numbers ending with 11,12,13", () => { + expect(getOrdinalNumber(1013313)).toEqual("1013313th"); + expect(getOrdinalNumber(2012)).toEqual("2012th"); + expect(getOrdinalNumber(16711)).toEqual("16711th"); +}); +//case 4;Numbers ending with 2 +test("should append 'nd' for numbers ending with 2", () => { + expect(getOrdinalNumber(102)).toEqual("102nd"); + expect(getOrdinalNumber(2022)).toEqual("2022nd"); + expect(getOrdinalNumber(16732)).toEqual("16732nd"); +}); +//case 4;Numbers ending with 3 +test("should append 'nd' for numbers ending with 3", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(2023)).toEqual("2023rd"); + expect(getOrdinalNumber(16733)).toEqual("16733rd"); +}); \ No newline at end of file From 21c61304554b5209d3579a9adb36963a1ebe99d2 Mon Sep 17 00:00:00 2001 From: Rizqah Date: Sat, 11 Jul 2026 20:01:27 +0100 Subject: [PATCH 2/2] repeat string implemented and tests written --- Sprint-3/2-practice-tdd/repeat-str.js | 9 +++-- Sprint-3/2-practice-tdd/repeat-str.test.js | 42 ++++++++++++++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 2af0a2cea7..917a69203a 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,7 +1,12 @@ -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"; + if(count >= 0){ + return str.repeat(count); + } + else{ + throw new Error("Invalid count"); + } } module.exports = repeatStr; diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index a3fc1196c4..c32d77eb3a 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -15,18 +15,56 @@ test("should repeat the string count times", () => { const repeatedStr = repeatStr(str, count); expect(repeatedStr).toEqual("hellohellohello"); }); +test("should repeat the string count times", () => { + const str = "codeyourfuture"; + const count = 2; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("codeyourfuturecodeyourfuture"); +}); // 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. - +test("should repeat the string count times", () => { + const str = "hello"; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("hello"); +}); +test("should repeat the string count times", () => { + const str = "codeyourfuture"; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("codeyourfuture"); +}); // 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 repeat the string count times", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual(""); +}); +test("should repeat the string count times", () => { + const str = "codeyourfuture"; + 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 repeat the string count times", () => { + const str = "hello"; + const count = -4; + expect(() => repeatStr(str, count)).toThrow("Invalid count"); +}); +test("should repeat the string count times", () => { + const str = "codeyourfuture"; + const count = -1; + expect(() => repeatStr(str, count)).toThrow("Invalid count"); +}); \ No newline at end of file