diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..d59e425d6b 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 count = 0; + for (let i = 0; i < stringOfCharacters.length; i++) { + if (stringOfCharacters[i] === 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..aab0e7ed52 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -17,7 +17,54 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(5); }); +test("should count multiple occurrences of a character", () => { + const str = "Code your Future"; + const char = "u"; + const count = countChar(str, char); + expect(count).toEqual(3); +}); + +test("should count multiple occurrences of a character", () => { + const str = "Introduction to Programming"; + const char = "o"; + const count = countChar(str, char); + expect(count).toEqual(4); +}); + +test("should count multiple occurrences of a character", () => { + const str = "Introduction to Javascript"; + const char = "i"; + const count = countChar(str, char); + expect(count).toEqual(2); +}); // Scenario: No Occurrences +test("should return 0 when the character does not occur in the string", () => { + const str = "No occurrences here"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); +// for empty string scenario. +test("should return 0 when the input string is empty", () => { + const str = ""; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); +//Scenario: for empty find character. +test("should return 0 when the find character is empty", () => { + const str = "empty find character"; + const char = ""; + const count = countChar(str, char); + expect(count).toEqual(0); +}); +// Scenario: counting empty spaces in a string. +test("should return the count of empty spaces in a string", () => { + const str = "The quick brown fox jumps over the lazy dog"; + const char = " "; + const count = countChar(str, char); + expect(count).toEqual(8); +}); // 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..bf1e98d8c8 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,8 @@ function getOrdinalNumber(num) { - return "1st"; + const s = ["th", "st", "nd", "rd"]; + const lastDigits = num % 100; + // Uses the index 1, 2, or 3 for st/nd/rd, defaults to 0 (th) + return num + (s[(lastDigits - 20) % 10] || s[lastDigits] || s[0]); } 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..a4644a72e5 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,27 @@ 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 numbers ending with 4-9, 0, and teens (11-13)", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(5)).toEqual("5th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(70)).toEqual("70th"); + expect(getOrdinalNumber(40)).toEqual("40th"); +}); + diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 2af0a2cea7..f8d85061b7 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,7 +1,8 @@ -function repeatStr() { - // 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"; +function repeatStr(str, count) { + let result = ""; + for (let i = 0; i < count; i++) { + result += str; + } + return result; } - 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..c91e48c013 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -9,14 +9,34 @@ const repeatStr = require("./repeat-str"); // When the repeatStr function is called with these inputs, // Then it should return a string that contains the original `str` repeated `count` times. -test("should repeat the string count times", () => { +test("should repeat the string with no spaces 3 times when count is 3", () => { const str = "hello"; const count = 3; const repeatedStr = repeatStr(str, count); expect(repeatedStr).toEqual("hellohellohello"); }); -// Case: handle count of 1: +test("should repeat the string with no spaces 3 times when count is 3", () => { + const str = "CYF"; + const count = 3; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("CYFCYFCYF"); +}); + +test("should display the string with spaces one times where count is 1", () => { + const str = "Code Your Future"; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("Code Your Future"); +}); + +test("should return empty string when count is 0 times", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual(""); +}); + // 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.