Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 21 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,25 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let arrayOfCharacters = [];
if (typeof stringOfCharacters === "string") {
arrayOfCharacters = stringOfCharacters.split("");
} else {
throw new Error("Input str should be a string");
}

if (typeof findCharacter !== "string") {
throw new Error("Input char should be a string");
}
if (findCharacter.length !== 1) {
throw new Error("Input char should be a single character");
}

let count = 0;
for (let index = 0; index < arrayOfCharacters.length; index++) {
if (arrayOfCharacters[index] === findCharacter) {
count += 1;
}
}
return count;
}

module.exports = countChar;
140 changes: 139 additions & 1 deletion 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,11 +14,149 @@ 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
// 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 character does not exist in the string", () => {
const str = "abcdefg";
const char = "h";
const count = countChar(str, char);
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);
});

// tests for unhappy paths (invalid str)
const errorMessageForString = "Input str should be a string";

// test for str is an array
test("should throw an error when str is an array", () => {
const str = ["a", "b", "c"];
const char = "a";
expect(() => countChar(str, char)).toThrow(errorMessageForString);
});

// test for str is a number
test("should throw an error when str is a number", () => {
const str = 12345;
const char = "1";
expect(() => countChar(str, char)).toThrow(errorMessageForString);
});

// test for str is an object
test("should throw an error when str is an object", () => {
const str = { a: 1, b: 2 };
const char = "a";
expect(() => countChar(str, char)).toThrow(errorMessageForString);
});

// test for str is null
test("should throw an error when str is null", () => {
const str = null;
const char = "a";
expect(() => countChar(str, char)).toThrow(errorMessageForString);
});

// test for str is undefined
test("should throw an error when str is undefined", () => {
const str = undefined;
const char = "a";
expect(() => countChar(str, char)).toThrow(errorMessageForString);
});

// test for str is a boolean
test("should throw an error when str is a boolean", () => {
const str = true;
const char = "a";
expect(() => countChar(str, char)).toThrow(errorMessageForString);
});

// tests for unhappy paths (invalid char)
const errorMessageForCharacter = "Input char should be a string";

// test for char is an array
test("should throw an error when char is an array", () => {
const str = "abcdefg";
const char = ["a", "b"];
expect(() => countChar(str, char)).toThrow(errorMessageForCharacter);
});

// test for char is a number
test("should throw an error when char is a number", () => {
const str = "abcdefg";
const char = 1;
expect(() => countChar(str, char)).toThrow(errorMessageForCharacter);
});

// test for char is an object
test("should throw an error when char is an object", () => {
const str = "abcdefg";
const char = { a: 1 };
expect(() => countChar(str, char)).toThrow(errorMessageForCharacter);
});

// test for char is null
test("should throw an error when char is null", () => {
const str = "abcdefg";
const char = null;
expect(() => countChar(str, char)).toThrow(errorMessageForCharacter);
});

// test for char is undefined
test("should throw an error when char is undefined", () => {
const str = "abcdefg";
const char = undefined;
expect(() => countChar(str, char)).toThrow(errorMessageForCharacter);
});

// test for char is a boolean
test("should throw an error when char is a boolean", () => {
const str = "abcdefg";
const char = true;
expect(() => countChar(str, char)).toThrow(errorMessageForCharacter);
});

// test for char is more than one character
const errorMessageForSingleCharacter =
"Input char should be a single character";

test("should throw an error when char is more than one character", () => {
const str = "abcdefg";
const char = "ab";
expect(() => countChar(str, char)).toThrow(errorMessageForSingleCharacter);
});

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

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

//test for char is an empty string
test("should throw an error when char is an empty string", () => {
const str = "abcdefg";
const char = "";
expect(() => countChar(str, char)).toThrow(errorMessageForSingleCharacter);
});
18 changes: 17 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,21 @@
function getOrdinalNumber(num) {
return "1st";
const stringNum = String(num);
const paddedLast2Digit = stringNum.padStart(2, "0").slice(-2);
const numberValue = Number(paddedLast2Digit);

if (numberValue >= 11 && numberValue <= 13) {
return `${stringNum}th`;
}
if (stringNum.endsWith("1")) {
return `${stringNum}st`;
}
if (stringNum.endsWith("2")) {
return `${stringNum}nd`;
}
if (stringNum.endsWith("3")) {
return `${stringNum}rd`;
}
return `${stringNum}th`;
}

module.exports = getOrdinalNumber;
60 changes: 60 additions & 0 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,63 @@ const getOrdinalNumber = require("./get-ordinal-number");
test("should return '1st' for 1", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
});
test("should return '2nd' for 2", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
});
test("should return '3rd' for 3", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
});
test("should return '4th' for 4", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
});
test("should return '11th' for 11", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
});
test("should return '12th' for 12", () => {
expect(getOrdinalNumber(12)).toEqual("12th");
});
test("should return '13th' for 13", () => {
expect(getOrdinalNumber(13)).toEqual("13th");
});
test("should return '21st' for 21", () => {
expect(getOrdinalNumber(21)).toEqual("21st");
});
test("should return '22nd' for 22", () => {
expect(getOrdinalNumber(22)).toEqual("22nd");
});
test("should return '23rd' for 23", () => {
expect(getOrdinalNumber(23)).toEqual("23rd");
});
test("should return '24th' for 24", () => {
expect(getOrdinalNumber(24)).toEqual("24th");
});
test("should return '111th' for 111", () => {
expect(getOrdinalNumber(111)).toEqual("111th");
});
test("should return '112th' for 112", () => {
expect(getOrdinalNumber(112)).toEqual("112th");
});
test("should return '113th' for 113", () => {
expect(getOrdinalNumber(113)).toEqual("113th");
});
test("should return '121st' for 121", () => {
expect(getOrdinalNumber(121)).toEqual("121st");
});
test("should return '1000011th' for 1000011", () => {
expect(getOrdinalNumber(1000011)).toEqual("1000011th");
});
test("should return '1000002nd' for 1000002", () => {
expect(getOrdinalNumber(1000002)).toEqual("1000002nd");
});
test("should return '0th' for 0", () => {
expect(getOrdinalNumber(0)).toEqual("0th");
});
test("should return '-1st' for -1", () => {
expect(getOrdinalNumber(-1)).toEqual("-1st");
});
test("should return '-2nd' for -2", () => {
expect(getOrdinalNumber(-2)).toEqual("-2nd");
});
test("should return '-3rd' for -3", () => {
expect(getOrdinalNumber(-3)).toEqual("-3rd");
});
10 changes: 8 additions & 2 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function repeat() {
return "hellohellohello";
function repeat(str, count) {
if (typeof str !== "string") {
throw new Error("Input str should be a string in format 'Hello'");
}
if (!Number.isInteger(count) || count <= 0) {
throw new Error("Count should be a positive integer number");
}
return str.repeat(count);
}

module.exports = repeat;
Loading