From 4e088d13c25a67d803e1b502298e01001096552c Mon Sep 17 00:00:00 2001 From: Asanda Date: Fri, 3 Jul 2026 22:43:10 +0200 Subject: [PATCH] Complete practice-tdd assignments cleanly from main --- Sprint-3/2-practice-tdd/count.js | 9 +++++++- Sprint-3/2-practice-tdd/count.test.js | 9 +++++++- Sprint-3/2-practice-tdd/get-ordinal-number.js | 20 +++++++++++++++-- .../2-practice-tdd/get-ordinal-number.test.js | 9 ++++++++ Sprint-3/2-practice-tdd/repeat-str.js | 10 +++++++-- Sprint-3/2-practice-tdd/repeat-str.test.js | 22 +++++++++++++++++++ 6 files changed, 73 insertions(+), 6 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..ccde6087d8 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,12 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + 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 179ea0ddf7..360a578f6a 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -17,7 +17,14 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(5); }); -// Scenario: No Occurrences +//Scenario: No Occurrences +test("should return 0 when the character does not exist in the string", () => { + const str = "hello"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + // Given the input string `str`, // And a character `char` that does not exist within `str`. // When the function is called with these inputs, diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db13..ac724a2d5b 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,21 @@ function getOrdinalNumber(num) { - return "1st"; -} + let lastDigit = num % 10; + let lastTwoDigits = num % 100; + if (lastTwoDigits === 11 || lastTwoDigits === 12 || lastTwoDigits === 13) { + return `${num}th`; + } + if (lastDigit === 1) { + return `${num}st`; + } + else if (lastDigit === 2) { + return `${num}nd`; + } + else if (lastDigit === 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..7e118afdd4 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,12 @@ 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("converts 1 to an ordinal number", () => { + expect(getOrdinalNumber(1)).toEqual("1st"); + expect(getOrdinalNumber(11)).toEqual("11th"); +}); +test("converts numbers to ordinal number", () => { + expect(getOrdinalNumber(21)).toEqual("21st"); + expect(getOrdinalNumber(33)).toEqual("33rd"); + expect(getOrdinalNumber(13)).toEqual("13th"); +}); \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 2af0a2cea7..92310e3f6b 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,7 +1,13 @@ -function repeatStr() { +function repeatStr(str, count) { + let result = ""; + 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. +// Case: handle count of 1: +test("should return the original string unchanged when count is 1", () => { + const str = "apple"; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("apple"); +}); + // 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 when count is 0", () => { + const str = "world"; + 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 when count is a negative integer", () => { + const str = "test"; + const count = -5; + expect(() => repeatStr(str, count)).toThrow(); +}); +