Skip to content
20 changes: 19 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
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;

}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you improve the indentation?

Copy link
Author

Choose a reason for hiding this comment

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

Can you improve the indentation?

Thank you, I have improved the indentation.


module.exports = countChar;

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

// Added lines to check cases and return count of characters.
52 changes: 48 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,51 @@ 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(2);

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

// 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
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
35 changes: 34 additions & 1 deletion 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,39 @@ const getOrdinalNumber = require("./get-ordinal-number");
// When the number is 1,
// Then the function should return "1st"

test("should return '1st' for 1", () => {
// Case 1: 1 → Standard ordinals

test("should return correct ordinals for 1, 2, 3, 4, 21, 22, 23, 24", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(24)).toEqual("24th");
});

// Case 2: Exceptions 11, 12, 13
test("should handle exceptions 11, 12, 13 correctly", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(111)).toEqual("111th");
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Could consider grouping test values by their characteristics. For examples,

  • All numbers that end in 1 (but not in 11).
  • Repeat the above for 2 and 3.
  • All numbers that end in 11, 12, or 13.
  • All numbers that end in 0, 4-9.
    (In addition to invalid cases).

This approach could better cover all possible cases and give more specific message when a test fails.

Copy link
Author

Choose a reason for hiding this comment

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

Could consider grouping test values by their characteristics. For examples,

* All numbers that end in 1 (but not in 11).

* Repeat the above for 2 and 3.

* All numbers that end in 11, 12, or 13.

* All numbers that end in 0, 4-9.
  (In addition to invalid cases).

This approach could better cover all possible cases and give more specific message when a test fails.

Thank you, I have tried to group them with exceptions.


// Case 3: Large numbers
test("should handle larger numbers correctly", () => {
expect(getOrdinalNumber(101)).toEqual("101st");
expect(getOrdinalNumber(112)).toEqual("112th");
expect(getOrdinalNumber(123)).toEqual("123rd");
});

// Case 4: 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
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