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
3 changes: 2 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
if (findCharacter.length === 0 || stringOfCharacters.length === 0) return 0;
else return stringOfCharacters.split(findCharacter).length - 1;
}

module.exports = countChar;
77 changes: 75 additions & 2 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const countChar = require("./count");
// When the countChar function is called with these inputs,
// Then it should:

// Scenario: Multiple Occurrences
// Scenario 1: Multiple Occurrences
// Given the input string `str`,
// And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'),
// When the function is called with these inputs,
Expand All @@ -17,8 +17,81 @@ test("should count multiple occurrences of a character", () => {
expect(count).toEqual(5);
});

// Scenario: No Occurrences
// Scenario 2: No Occurrences
// Given the input string `str`,
// And a character `char` that does not exist within `str`.
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of `char` were found.

test("should return 0 if there are no occurrences of a character", () => {
const str = "there is no two number here";
const char = "2";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Scenario 3: Single Occurrence
// Given the input string `str`,
// And a character `char` that exists only once within `str`.
// When the function is called with these inputs,
// Then it should return 1, indicating that single occurrence of `char` were found.

test("should return 1 if there is single occurrence of a character", () => {
const str = " let's find if we got what you are looking for";
const char = "'";
const count = countChar(str, char);
expect(count).toEqual(1);
});

// Scenario 4: Zero occurrence when the string is empty
// Given the input string `str` which is empty,
// And a character `char` that can't be existed in the `str` as it is empty.
// When the function is called with these inputs,
// Then it should return 0, indicating that empty string can't hold that char.

test("should return 0 as the string is empty", () => {
const str = "";
const char = "2";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Scenario 5: Multiple occurrences if the string contains single empty spaces and our char is an empty space
// Given the input string `str` that contains ' ' empty spaces,
// And a character `char` which is just single empty space ' '.
// When the function is called with these inputs,
// Then it should return the number of occurrences of single empty spaces in the string.

test("should return multiple occurrences of empty spaces in the string", () => {
const str = "Hi I have got ten single empty spaces in this string.";
const char = " ";
const count = countChar(str, char);
expect(count).toEqual(10);
});

// Scenario 6: No occurrences if the char is just an empty char
// Given the input string `str` that contains any number of characters or no characters at all,
// And a character `char` which is empty ''.
// When the function is called with these inputs,
// Then it should return 0 as there is nothing to count as the char is just empty.

test("should return 0 as the char is just empty", () => {
const str =
"Hi I am a string that can be empty or not empty but I can't count empty characters.";
const char = "";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Scenario 7: Multiple Occurrences at different places
// Given the input string `str`,
// And a character `char` that occurs more than one times in `str` (e.g., 'a' in 'add the angle to the antenna'),
// When the function is called with these inputs,
// Then it should correctly count occurrences of `char` a in the string.

test("should count multiple occurrences of a character", () => {
const str = "add the angle to the antenna";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(4);
});
9 changes: 8 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,12 @@
function getOrdinalNumber(num) {
return "1st";
let numberString = String(num);
lastTwoCharactersNumberString = numberString.slice(-2);
lastCharacterNumberString = numberString.slice(-1);
if (lastTwoCharactersNumberString === "11") return num + "th";
else if (lastCharacterNumberString === "1") return num + "st";
else if (lastCharacterNumberString === "2") return num + "nd";
else if (lastCharacterNumberString === "3") return num + "rd";
else return num + "th";
}

module.exports = getOrdinalNumber;
45 changes: 45 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 @@ -17,4 +17,49 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
expect(getOrdinalNumber(-171)).toEqual("-171st");
});

// Case 2: Special case for number 11)
// When the number is 11 or the numbers ends with 11,
// Then the function should return a string by appending "th" to the number.
test("should append 'th' for number 11", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(111)).toEqual("111th");
expect(getOrdinalNumber(12211)).toEqual("12211th");
expect(getOrdinalNumber(98011)).toEqual("98011th");
expect(getOrdinalNumber(-87611)).toEqual("-87611th");
});

// Case 3: Numbers ending with 2
// When the number is ending with 2,
// Then the function should return a string by appending "nd" to the number.
test("should append 'st' for numbers ending with 2", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(152)).toEqual("152nd");
expect(getOrdinalNumber(-82)).toEqual("-82nd");
});

// Case 4: Numbers ending with 3
// When the number is ending with 3,
// Then the function should return a string by appending "rd" to the number.
test("should append 'st' for numbers ending with 3", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(33)).toEqual("33rd");
expect(getOrdinalNumber(183)).toEqual("183rd");
expect(getOrdinalNumber(-903)).toEqual("-903rd");
});

// Case 5: Numbers not ending 1, 2 and 3 except 11
// When the number is not ending with 1, 2 and 3 except 11,
// Then the function should return a string by appending "th" to the number.
test("should append 'th' for numbers not ending with 1, 2 and 3 except those ending with 11", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(0)).toEqual("0th");
expect(getOrdinalNumber(125)).toEqual("125th");
expect(getOrdinalNumber(1000)).toEqual("1000th");
expect(getOrdinalNumber(87939)).toEqual("87939th");
expect(getOrdinalNumber(-780987)).toEqual("-780987th");
});
7 changes: 5 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
function repeatStr() {
return "hellohellohello";
function repeatStr(str, count) {
if (count === 0) return "";
else if (count === 1) return str;
else if (count < 0) throw new Error("Count can't be negative");
else return str.repeat(count);
}

module.exports = repeatStr;
19 changes: 19 additions & 0 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,32 @@ test("should repeat the string count times", () => {
// 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.
test("should repeat the original string as the count is 1", () => {
const str = "OneWord";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("OneWord");
});

// Case: Handle count of 0:
// Given a target string `str` and a `count` equal to 0,
// When the repeatStr function is called with these inputs,
// Then it should return an empty string.
test("should return an empty string as the count is 0", () => {
const str = "hello";
const count = 0;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("");
});

// Case: Handle negative count:
// Given a target string `str` and a negative integer `count`,
// When the repeatStr function is called with these inputs,
// Then it should throw an error, as negative counts are not valid.
test("should throw an error when count is a negative number", () => {
const str = "hello";
const count = -2;
expect(() => {
repeatStr(str, count);
}).toThrow("Count can't be negative");
});
Loading