From 2c335f3bb82246dc3361847327ca097d83dcccb2 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Thu, 15 Oct 2026 12:37:38 +0100 Subject: [PATCH 1/6] code work for test --- practice-tdd/count.js | 10 +++++++++- practice-tdd/count.test.js | 11 +++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/practice-tdd/count.js b/practice-tdd/count.js index 5518c5712..4972a004f 100644 --- a/practice-tdd/count.js +++ b/practice-tdd/count.js @@ -1,3 +1,11 @@ export function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + let position = stringOfCharacters.indexOf(findCharacter); + + while (position !== -1) { + count++; + position = stringOfCharacters.indexOf(findCharacter, position + 1); + } + + return count; } diff --git a/practice-tdd/count.test.js b/practice-tdd/count.test.js index cb3aaae59..42bd4b95d 100644 --- a/practice-tdd/count.test.js +++ b/practice-tdd/count.test.js @@ -11,10 +11,17 @@ import { countChar } from "./count.js"; // Then it should correctly count occurrences of `char`. test("should count multiple occurrences of a character", () => { - const str = "aaaaa"; + const str = "hello"; const char = "a"; const count = countChar(str, char); - expect(count).toEqual(5); + expect(count).toEqual(0); +}); + +test("should count multiple occurrences of a character", () => { + const str = "This ia a second string test"; + const char = "s"; + const count = countChar(str, char); + expect(count).toEqual(4); }); // Scenario: No Occurrences From 5ed89142db589a42172ca9bb354446010723d9f3 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Thu, 15 Oct 2026 13:20:02 +0100 Subject: [PATCH 2/6] check every edge case --- practice-tdd/count.js | 14 ++++++++------ practice-tdd/count.test.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/practice-tdd/count.js b/practice-tdd/count.js index 4972a004f..7b6986706 100644 --- a/practice-tdd/count.js +++ b/practice-tdd/count.js @@ -1,11 +1,13 @@ export function countChar(stringOfCharacters, findCharacter) { let count = 0; let position = stringOfCharacters.indexOf(findCharacter); - - while (position !== -1) { - count++; - position = stringOfCharacters.indexOf(findCharacter, position + 1); + if (position === -1) { + return 0; + } else { + while (position !== -1) { + count++; + position = stringOfCharacters.indexOf(findCharacter, position + 1); + } + return count; } - - return count; } diff --git a/practice-tdd/count.test.js b/practice-tdd/count.test.js index 42bd4b95d..9e8088045 100644 --- a/practice-tdd/count.test.js +++ b/practice-tdd/count.test.js @@ -17,6 +17,20 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(0); }); +test("should count multiple occurrences of a character", () => { + const str = "aaaaa"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(5); +}); + +test("should count multiple occurrences of a character", () => { + const str = "Shirin"; + const char = "h"; + const count = countChar(str, char); + expect(count).toEqual(1); +}); + test("should count multiple occurrences of a character", () => { const str = "This ia a second string test"; const char = "s"; @@ -24,6 +38,20 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(4); }); +test("should count multiple occurrences of a character", () => { + const str = ""; + const char = "t"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +test("should count multiple occurrences of a character", () => { + const str = "TTTT"; + const char = "t"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + // Scenario: No Occurrences // Given the input string `str`, // And a character `char` that does not exist within `str`. From 3efdae454faefb99e1518ab028c0b1badd57c877 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Thu, 15 Oct 2026 19:02:36 +0100 Subject: [PATCH 3/6] both condition is working --- practice-tdd/get-ordinal-number.js | 7 ++++++- practice-tdd/get-ordinal-number.test.js | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/practice-tdd/get-ordinal-number.js b/practice-tdd/get-ordinal-number.js index 5c6291f5e..7c41c7e65 100644 --- a/practice-tdd/get-ordinal-number.js +++ b/practice-tdd/get-ordinal-number.js @@ -1,3 +1,8 @@ export function getOrdinalNumber(num) { - return "1st"; + if (num % 100 === 11) { + throw new Error("Input must not ending with 11"); + } + if (num % 10 === 1) { + return `${num}st`; + } } diff --git a/practice-tdd/get-ordinal-number.test.js b/practice-tdd/get-ordinal-number.test.js index e23174a18..7c823c3cc 100644 --- a/practice-tdd/get-ordinal-number.test.js +++ b/practice-tdd/get-ordinal-number.test.js @@ -1,4 +1,5 @@ import { getOrdinalNumber } from "./get-ordinal-number.js"; +import { test, expect } from "@jest/globals"; // In this week's prep, we started implementing getOrdinalNumber. // Continue testing and implementing getOrdinalNumber for additional cases. @@ -11,6 +12,11 @@ import { getOrdinalNumber } from "./get-ordinal-number.js"; // This approach improves coverage and makes our tests easier to maintain. // Case 1: Numbers ending with 1 (but not 11) +test("should not append 'st' for number ending with 11 ", () => { + expect(() => getOrdinalNumber(11)).toThrow("Input must not ending with 11"); + expect(() => getOrdinalNumber(111)).toThrow("Input must not ending with 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", () => { From 472c7284b8bcc159598baa7470a6d50d8b2b0a68 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Thu, 15 Oct 2026 19:28:33 +0100 Subject: [PATCH 4/6] Implement st, nd, rd, and th suffixes --- practice-tdd/get-ordinal-number.js | 17 ++++++++++ practice-tdd/get-ordinal-number.test.js | 44 +++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/practice-tdd/get-ordinal-number.js b/practice-tdd/get-ordinal-number.js index 7c41c7e65..36658eda9 100644 --- a/practice-tdd/get-ordinal-number.js +++ b/practice-tdd/get-ordinal-number.js @@ -5,4 +5,21 @@ export function getOrdinalNumber(num) { if (num % 10 === 1) { return `${num}st`; } + + if (num % 100 === 22) { + throw new Error("Input must not ending with 22"); + } + + if (num % 10 === 2) { + return `${num}nd`; + } + if (num % 100 === 33) { + throw new Error("Input must not ending with 33"); + } + + if (num % 10 === 3) { + return `${num}rd`; + } + + return `${num}th`; } diff --git a/practice-tdd/get-ordinal-number.test.js b/practice-tdd/get-ordinal-number.test.js index 7c823c3cc..3b4793978 100644 --- a/practice-tdd/get-ordinal-number.test.js +++ b/practice-tdd/get-ordinal-number.test.js @@ -15,6 +15,9 @@ import { test, expect } from "@jest/globals"; test("should not append 'st' for number ending with 11 ", () => { expect(() => getOrdinalNumber(11)).toThrow("Input must not ending with 11"); expect(() => getOrdinalNumber(111)).toThrow("Input must not ending with 11"); + expect(() => getOrdinalNumber(45711)).toThrow( + "Input must not ending with 11" + ); }); // When the number ends with 1, except those ending with 11, @@ -23,4 +26,45 @@ 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(241)).toEqual("241st"); +}); + +// Case 2: Numbers ending with 2 (but not 22) +// When the number ends with 2, except those ending with 22, +// Then the function should return a string by appending "nd" to the number. +test("should append 'nd' for numbers ending with 2, except those ending with 22", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(32)).toEqual("32nd"); + expect(getOrdinalNumber(132)).toEqual("132nd"); +}); + +test("should not append 'nd' for number ending with 22 ", () => { + expect(() => getOrdinalNumber(22)).toThrow("Input must not ending with 22"); + expect(() => getOrdinalNumber(122)).toThrow("Input must not ending with 22"); + expect(() => getOrdinalNumber(45722)).toThrow( + "Input must not ending with 22" + ); +}); + +// Case 3: Numbers ending with 3 (but not 33) +test("should append 'rd' for numbers ending with 3, except those ending with 33", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(13)).toEqual("13rd"); + expect(getOrdinalNumber(123)).toEqual("123rd"); +}); + +test("should not append 'rd' for number ending with 33 ", () => { + expect(() => getOrdinalNumber(33)).toThrow("Input must not ending with 33"); + expect(() => getOrdinalNumber(133)).toThrow("Input must not ending with 33"); + expect(() => getOrdinalNumber(45733)).toThrow( + "Input must not ending with 33" + ); +}); + +// Case 3: Numbers ending in other digits +test("should append 'th' for numbers ending in other digits", () => { + expect(getOrdinalNumber(4)).toBe("4th"); + expect(getOrdinalNumber(5)).toBe("5th"); + expect(getOrdinalNumber(10)).toBe("10th"); + expect(getOrdinalNumber(20)).toBe("20th"); }); From f5154ef169d565321254b8e53e909182534ebae4 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Fri, 16 Oct 2026 11:31:18 +0100 Subject: [PATCH 5/6] complete repeat function and jest test --- practice-tdd/repeat-str.js | 11 +++++++++-- practice-tdd/repeat-str.test.js | 28 +++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/practice-tdd/repeat-str.js b/practice-tdd/repeat-str.js index af273f3b0..b9b241cfe 100644 --- a/practice-tdd/repeat-str.js +++ b/practice-tdd/repeat-str.js @@ -1,5 +1,12 @@ -export function repeatStr() { +export 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) { + throw new Error("negative counts are not valid"); + } + let strRep = ""; + for (let i = 0; i < count; i++) { + strRep += str; + } + return strRep; } diff --git a/practice-tdd/repeat-str.test.js b/practice-tdd/repeat-str.test.js index 8a7036102..305c3dc5f 100644 --- a/practice-tdd/repeat-str.test.js +++ b/practice-tdd/repeat-str.test.js @@ -1,5 +1,6 @@ // Implement a function repeatStr import { repeatStr } from "./repeat-str.js"; +import { test, expect } from "@jest/globals"; // Given a target string `str` and a positive integer `count`, // When the repeatStr function is called with these inputs, // Then it should: @@ -15,18 +16,39 @@ test("should repeat the string count times", () => { const repeatedStr = repeatStr(str, count); expect(repeatedStr).toEqual("hellohellohello"); }); - // 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 = "World"; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("World"); +}); // 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 an empty string for zero", () => { + const str = "Hi!"; + 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 throw an error, as negative counts are not valid", () => { + const str = "Hi!"; + const count = -4; + expect(() => repeatStr(str, -4)).toThrow("negative counts are not valid"); +}); + +test("should repeat the string count times", () => { + const str = ""; + const count = 5; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual(""); +}); From df91c9f67ed156c8b6a70ecc8c7b03c04eca0a88 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Thu, 22 Oct 2026 23:28:22 +0100 Subject: [PATCH 6/6] cover ordinal suffix exceptions and edge cases --- practice-tdd/get-ordinal-number.js | 16 ++------- practice-tdd/get-ordinal-number.test.js | 44 ++++++++----------------- 2 files changed, 16 insertions(+), 44 deletions(-) diff --git a/practice-tdd/get-ordinal-number.js b/practice-tdd/get-ordinal-number.js index 36658eda9..bf0bee67f 100644 --- a/practice-tdd/get-ordinal-number.js +++ b/practice-tdd/get-ordinal-number.js @@ -1,23 +1,13 @@ export function getOrdinalNumber(num) { - if (num % 100 === 11) { - throw new Error("Input must not ending with 11"); - } - if (num % 10 === 1) { + if (num % 10 === 1 && num % 100 != 11) { return `${num}st`; } - if (num % 100 === 22) { - throw new Error("Input must not ending with 22"); - } - - if (num % 10 === 2) { + if (num % 10 === 2 && num % 100 != 12) { return `${num}nd`; } - if (num % 100 === 33) { - throw new Error("Input must not ending with 33"); - } - if (num % 10 === 3) { + if (num % 10 === 3 && num % 100 != 13) { return `${num}rd`; } diff --git a/practice-tdd/get-ordinal-number.test.js b/practice-tdd/get-ordinal-number.test.js index 3b4793978..c54ac202c 100644 --- a/practice-tdd/get-ordinal-number.test.js +++ b/practice-tdd/get-ordinal-number.test.js @@ -1,5 +1,4 @@ import { getOrdinalNumber } from "./get-ordinal-number.js"; -import { test, expect } from "@jest/globals"; // In this week's prep, we started implementing getOrdinalNumber. // Continue testing and implementing getOrdinalNumber for additional cases. @@ -12,13 +11,6 @@ import { test, expect } from "@jest/globals"; // This approach improves coverage and makes our tests easier to maintain. // Case 1: Numbers ending with 1 (but not 11) -test("should not append 'st' for number ending with 11 ", () => { - expect(() => getOrdinalNumber(11)).toThrow("Input must not ending with 11"); - expect(() => getOrdinalNumber(111)).toThrow("Input must not ending with 11"); - expect(() => getOrdinalNumber(45711)).toThrow( - "Input must not ending with 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. @@ -29,42 +21,32 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" expect(getOrdinalNumber(241)).toEqual("241st"); }); -// Case 2: Numbers ending with 2 (but not 22) -// When the number ends with 2, except those ending with 22, +// Case 2: Numbers ending with 2 (but not 12) +// When the number ends with 2, except those ending with 12, // Then the function should return a string by appending "nd" to the number. -test("should append 'nd' for numbers ending with 2, except those ending with 22", () => { +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(32)).toEqual("32nd"); expect(getOrdinalNumber(132)).toEqual("132nd"); }); -test("should not append 'nd' for number ending with 22 ", () => { - expect(() => getOrdinalNumber(22)).toThrow("Input must not ending with 22"); - expect(() => getOrdinalNumber(122)).toThrow("Input must not ending with 22"); - expect(() => getOrdinalNumber(45722)).toThrow( - "Input must not ending with 22" - ); -}); - -// Case 3: Numbers ending with 3 (but not 33) -test("should append 'rd' for numbers ending with 3, except those ending with 33", () => { +// Case 3: Numbers ending with 3 (but not 13) +test("should append 'rd' for numbers ending with 3, except those ending with 13", () => { expect(getOrdinalNumber(3)).toEqual("3rd"); - expect(getOrdinalNumber(13)).toEqual("13rd"); + expect(getOrdinalNumber(33)).toEqual("33rd"); expect(getOrdinalNumber(123)).toEqual("123rd"); }); -test("should not append 'rd' for number ending with 33 ", () => { - expect(() => getOrdinalNumber(33)).toThrow("Input must not ending with 33"); - expect(() => getOrdinalNumber(133)).toThrow("Input must not ending with 33"); - expect(() => getOrdinalNumber(45733)).toThrow( - "Input must not ending with 33" - ); -}); - // Case 3: Numbers ending in other digits test("should append 'th' for numbers ending in other digits", () => { - expect(getOrdinalNumber(4)).toBe("4th"); + expect(getOrdinalNumber(11)).toBe("11th"); + expect(getOrdinalNumber(12)).toBe("12th"); + expect(getOrdinalNumber(13)).toBe("13th"); expect(getOrdinalNumber(5)).toBe("5th"); expect(getOrdinalNumber(10)).toBe("10th"); expect(getOrdinalNumber(20)).toBe("20th"); + expect(getOrdinalNumber(111)).toBe("111th"); + expect(getOrdinalNumber(113)).toBe("113th"); + expect(getOrdinalNumber(112)).toBe("112th"); });