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

module.exports = countChar;
7 changes: 7 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ test("should count multiple occurrences of a character", () => {
expect(count).toEqual(5);
});

test("should count zero occurrences of a character", () => {
const str = "aaaaa";
const char = "b";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Scenario: No Occurrences
// Given the input string `str`,
// And a character `char` that does not exist within `str`.
Expand Down
4 changes: 3 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,7 @@
function getOrdinalNumber(num) {
return "1st";
if (num % 10 === 1 && num % 100 !== 11) {
return num + "st";
}
}

module.exports = getOrdinalNumber;
33 changes: 28 additions & 5 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Implement a function repeatStr
const repeatStr = require("./repeat-str");

// Given a target string `str` and a positive integer `count`,
// When the repeatStr function is called with these inputs,
// Then it should:
Expand All @@ -9,24 +9,47 @@ const repeatStr = require("./repeat-str");
// When the repeatStr function is called with these inputs,
// Then it should return a string that contains the original `str` repeated `count` times.

function repeatStr(str, count) {
if (count < 0) {
throw new Error("Count must be a non-negative integer");
} else if (count === 0) {
return "";
} else {
return str.repeat(count);
}
}

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

// Case: handle count of 1:
// 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 return the original string when count is 1", () => {
const str = "hello";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hello");
});


// 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 when 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 for a negative count", () => {
expect(() => repeatStr("hello", -1)).toThrow();

});
Loading