diff --git a/.gitignore b/.gitignore index bde36e5302..a4da420804 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ node_modules .DS_Store -.vscode -**/.DS_Store \ No newline at end of file +.vscode/ +**/.DS_Store diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..e10b6cf0f7 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..c3fd8ec5e5 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,15 @@ test("should count multiple occurrences of a character", () => { // 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", () => { + expect(countChar("aaaaa", "a")).toEqual(5); + expect(countChar("blind", "a")).toEqual(0); + expect(countChar("blood", "o")).toEqual(2); + expect(countChar("bbbrf", "b")).toEqual(3); + expect(countChar("ooooa", "o")).toEqual(4); +}); + +// handling invalid input +// the tests work assuming that only letters are in the string +// numbers and special characters are not tested for diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db13..57c5d1ade5 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,20 @@ function getOrdinalNumber(num) { - return "1st"; + // Check if input is a number + if (!Number.isInteger(num) || !Number.isFinite(num) || num <= 0) { + throw new Error("Input must be a valid number"); + } + + const lastNum = num % 10; // checks what last number is + const last2Num = num % 100; // checks what last two numbers are, needed to check for 11 + + if (last2Num >= 11 && last2Num <= 13) { + return num + "th"; + } + if (lastNum === 1) return num + "st"; + if (lastNum === 2) return num + "nd"; + if (lastNum === 3) return num + "rd"; + + 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..983476645f 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -13,8 +13,39 @@ 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 11", () => { expect(getOrdinalNumber(1)).toEqual("1st"); expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); }); + +// Case 2: Numbers ending with 2 +// When the number ends with 2, +// Then the function should return a string by appending "nd" to the number. + +test("should append 'nd' for numbers ending with 2 except 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(32)).toEqual("32nd"); + expect(getOrdinalNumber(252)).toEqual("252nd"); +}); + +// Case 3: Numbers ending with 3 +// When the number ends with 3, +// Then the function should return a string by appending "rd" to the number. + +test("should append 'rd' for numbers ending with 3 except 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(33)).toEqual("33rd"); + expect(getOrdinalNumber(133)).toEqual("133rd"); +}); + +// Case 4: The remaining numbers +// For numbers ending in 0, 4, 5, 6, 7, 8, 9, 11, 12, 13 +// the function should return a string by appending "th" to the number. + +test("should append 'th' if number is ending in 0, 4, 5,6, 7, 8, 9, 10, 11, 12 or 13", () => { + expect(getOrdinalNumber(20)).toEqual("20th"); + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(99)).toEqual("99th"); +}); diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 3838c7b003..7a21885ad2 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,5 +1,13 @@ -function repeatStr() { - return "hellohellohello"; +function repeatStr(str, count) { + if (count < 0) { + throw new Error("Count must be positive"); + } + + if (count === 0) { + return " "; + } + + return str.repeat(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..21f1102ada 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -5,11 +5,11 @@ const repeatStr = require("./repeat-str"); // Then it should: // Case: handle multiple repetitions: -// Given a target string `str` and a positive integer `count` greater than 1, -// When the repeatStr function is called with these inputs, -// Then it should return a string that contains the original `str` repeated `count` times. +// A string is repeated count number of times +// If a string is "hello" and count is 3 then +// the hello will be output 3 times with no spaces between -test("should repeat the string count times", () => { +test("when count equals 3 it should repeat the string 3 times without spaces", () => { const str = "hello"; const count = 3; const repeatedStr = repeatStr(str, count); @@ -17,16 +17,35 @@ test("should repeat the string count times", () => { }); // 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. +// If count is 1 then the string is not repeated and +// will be output for example if the string is "Hello" +// then the output will be "Hello" + +test("when count equals 1 it produce the string", () => { + const str = "hello"; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("hello"); +}); // 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. +// If the the string is empty then count will be 0 +// and an empty string will be returned + +test("should return an empty string when count = 0 ", () => { + const str = "hello"; + 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 negative", () => { + const str = "hello"; + const count = -3; + expect(() => repeatStr(str, count)).toThrow("Count must be positive"); +});