Skip to content
22 changes: 21 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
if (typeof stringOfCharacters != "string") {
throw new Error("Input should be a string");
}
if (typeof findCharacter !== "string" || findCharacter.length != 1) {
throw new Error("Input must be a single character");
}

let count = 0;

for (let char of stringOfCharacters) {
if (char === findCharacter) {
count++;
}
}
return count;
}

module.exports = countChar;

console.log(countChar("amazon", "a"));

// Added lines to check cases and return count of characters.
// Indentation improved.
// count.js formatted with prettier.
70 changes: 66 additions & 4 deletions Sprint-3/2-practice-tdd/count.test.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test is quite comprehensive.

Can your function pass all the tests?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test is quite comprehensive.

Can your function pass all the tests?

I have added, some more tests and the function passed all tests.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,69 @@ test("should count multiple occurrences of a character", () => {
});

// Scenario: No Occurrences
// Given the input string str,
// And a character char that does not exist within the case-sensitive str,
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
test("should return 0 when the character is not in the string", () => {
const str = "hello";
const char = "x";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Scenario: Case Sensitivity

test("should be case-sensitive when counting characters", () => {
const str = "Banana";
expect(countChar(str, "a")).toEqual(3);
expect(countChar(str, "A")).toEqual(0);
});

// Scenario: Empty String

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 : Special characters

test("should count special characters like spaces or punctuation", () => {
const str1 = "a b a b";
const char1 = " ";
expect(countChar(str1, char1)).toEqual(3);

const str2 = "NO !!!";
const char2 = "!";
expect(countChar(str2, char2)).toEqual(3);
});

// Scenario: Numeric characters inside string

test("should count numeric characters inside the string", () => {
const str3 = "123321";
const char3 = "2";
expect(countChar(str3, char3)).toEqual(2);
});

// Scenarion Accented characters

test("should count accented characters and unicode properly", () => {
const str4 = "café";
const char4 = "é";
expect(countChar(str4, char4)).toEqual(1);
});

// Scenario: Invalid inputs

test("should throw error for invalid string input", () => {
expect(() => countChar(123, "a")).toThrow("Input should be a string");
});

test("should throw error for invalid character input", () => {
expect(() => countChar("hello", "ab")).toThrow("Input must be a single character");
});


// Added different cases and test using npx jest

// Added some tests, additional test and passed the tests.
22 changes: 21 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
function getOrdinalNumber(num) {
return "1st";
if (!Number.isInteger(num) || num <= 0) {
throw new Error("Only positive integers are allowed");
}
// Handle Exception of 11, 12, 13
if (num % 100 >= 11 && num % 100 <= 13) {
return num + "th";
}
// Handle general last-digit cases
switch (num % 10) {
case 1:
return num + "st";
case 2:
return num + "nd";
case 3:
return num + "rd";
default:
return num + "th";
}
}


module.exports = getOrdinalNumber;

// Function implemented
55 changes: 53 additions & 2 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,57 @@ const getOrdinalNumber = require("./get-ordinal-number");
// When the number is 1,
// Then the function should return "1st"

test("should return '1st' for 1", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
// Case 1: Numbers ending in 1 except 11

test("should return 'st' for numbers ending in 1 except 11", () => {
const values = [1, 21, 31, 101, 1001];
values.forEach(num => {
expect(getOrdinalNumber(num)).toEqual(`${num}st`);
});
});

// Case 2: Numbers ending in 2 except 12

test("should return 'nd' for numbers ending in 2 except 12", () => {
const values = [2, 22, 32, 102, 1002];
values.forEach(num => {
expect(getOrdinalNumber(num)).toEqual(`${num}nd`);
});
});

// Case 3: Numbers ending in 3 except 13

test("should return 'rd' for numbers ending in 3 except 13", () => {
const values = [3, 23, 33, 103, 1003];
values.forEach(num => {
expect(getOrdinalNumber(num)).toEqual(`${num}rd`);
});
});

// Case 4: Exceptions 11, 12 & 13

test("should return 'th' for 11, 12, 13 regardless of other digits", () => {
const values = [11, 12, 13, 111, 212, 513];
values.forEach(num => {
expect(getOrdinalNumber(num)).toEqual(`${num}th`);
});
});

// Case 5: Numbers ending in 0 and (4-9)

test("should return 'th' for numbers ending in 0 or 4–9", () => {
const values = [4, 5, 6, 7, 8, 9, 10, 20, 25, 100, 104, 1009];
values.forEach(num => {
expect(getOrdinalNumber(num)).toEqual(`${num}th`);
});
});

// Case 6: Invalid cases / inputs
test("should throw error for zero, negative or non-integer inputs", () => {
expect(() => getOrdinalNumber(0)).toThrow("Only positive integers are allowed");
expect(() => getOrdinalNumber(-5)).toThrow("Only positive integers are allowed");
expect(() => getOrdinalNumber(2.5)).toThrow("Only positive integers are allowed");
});

// Added possible and invalid cases and tested using npx jest
// Modified the test into groups
14 changes: 12 additions & 2 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
function repeat() {
return "hellohellohello";
function repeat(str, count) {
if (typeof str !== "string") {
throw new Error("Input must be a string");
}
if (!Number.isInteger(count) || count < 0) {
throw new Error("Count must be a non-negative integer");
}

return str.repeat(count);
}

module.exports = repeat;


// Function implemented to repeat string count times.
41 changes: 29 additions & 12 deletions Sprint-3/2-practice-tdd/repeat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,41 @@ const repeat = require("./repeat");
// When the repeat function is called with these inputs,
// Then it should repeat the str count times and return a new string containing the repeated str values.

// Case 1: Repeats String coun times.

test("should repeat the string count times", () => {
const str = "hello";
const count = 3;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("hellohellohello");
});

// case: handle Count of 1:
// Given a target string str and a count equal to 1,
// When the repeat function is called with these inputs,
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
// case 2: handle Count of 1:

// case: Handle Count of 0:
// Given a target string str and a count equal to 0,
// When the repeat function is called with these inputs,
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
test("should return the original string when count is 1", () => {
const str = "hello";
const count = 1;
expect(repeat(str, count)).toEqual("hello");
});
// Returns the original str without repetition, ensuring that a count of 1 results in no repetition.

// case: Negative Count:
// Given a target string str and a negative integer count,
// When the repeat function is called with these inputs,
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
// case 3: Handle Count of 0:

test("should return an empty string when count is 0", () => {
const str = "hello";
const count = 0;
expect(repeat(str, count)).toEqual("");
});

// Returns an empty string, ensuring that a count of 0 results in an empty output.

// Case 4: Negative count

test("should throw an error when count is negative", () => {
const str = "hello";
const count = -2;
expect(() => repeat(str, count)).toThrow("Count must be a non-negative integer");
});
// Throws an error or return an appropriate error message, as negative counts are not valid.

// Tested for all case using npx jest