Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Sprint-3/2-practice-tdd/count.js

Choose a reason for hiding this comment

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

It might have been a leftover from debugging, but console logs should be removed before submitting your PR.

Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
function countChar(stringOfCharacters, findCharacter) {
const arrayOfCharacters = stringOfCharacters.split("");
let arrayOfCharacters = [];
if (typeof stringOfCharacters === "string") {
arrayOfCharacters = stringOfCharacters.split("");
} else if (Array.isArray(stringOfCharacters)) {
arrayOfCharacters = stringOfCharacters;
} else if (typeof stringOfCharacters === "number") {
arrayOfCharacters = stringOfCharacters.toString().split("");
} else if (typeof stringOfCharacters === "object") {
return 0;
}

let count = 0;
for (let index = 0; index < arrayOfCharacters.length; index++) {
if (arrayOfCharacters[index] === findCharacter) {
Expand Down
28 changes: 26 additions & 2 deletions Sprint-3/2-practice-tdd/count.test.js

Choose a reason for hiding this comment

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

Your test case covers the most basic cases - but can you think of some edge cases you should write tests for?

  • what is count is 2.5?
  • what if the count is null or undefined
  • what if the string is an array or an object instead of a string?

Writing tests to cover edge cases will protect your implementation from unexpected situations and make your code more robust.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test("should count multiple occurrences of a character", () => {
const str = "aaaaa";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(5);
expect(parseInt(count)).toEqual(5);
});

// Scenario: No Occurrences
Expand All @@ -27,5 +27,29 @@ test("should return 0 when character does not exist in the string", () => {
const str = "abcdefg";
const char = "h";
const count = countChar(str, char);
expect(count).toEqual(0);
expect(parseInt(count)).toEqual(0);
});

// test for empty string
test("should return 0 when string is empty", () => {
const str = "";
const char = "a";
const count = countChar(str, char);
expect(parseInt(count)).toEqual(0);
});

// test for str is an array
test("should return 0 when str is an array", () => {

Choose a reason for hiding this comment

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

Maybe a typo in your test assertion here (should be a 1 instead of 0)?

const str = ["a", "b", "c"];
const char = "a";
const count = countChar(str, char);
expect(parseInt(count)).toEqual(1);
});

// test for str is a number
test("should return 0 when str is a number", () => {

Choose a reason for hiding this comment

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

Maybe a typo in your test assertion here (should be a 1 instead of 0)?

const str = 12345;
const char = "3";
const count = countChar(str, char);
expect(parseInt(count)).toEqual(1);
});