Skip to content

Commit 7172fd2

Browse files
committed
I have implemented a function called repeat and writen a few different tests to check my function
1 parent 1382dc5 commit 7172fd2

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

Sprint-3/2-practice-tdd/repeat.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
function repeat() {
2-
return "hellohellohello";
1+
function repeat(myString, repeatNumber) {
2+
if (repeatNumber < 0) return "Invalid Input must be a positive number";
3+
let str = "";
4+
for (let i = 0; i < repeatNumber; i++) {
5+
str += myString;
6+
}
7+
return str;
38
}
49

510
module.exports = repeat;

Sprint-3/2-practice-tdd/repeat.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,31 @@ test("should repeat the string count times", () => {
2121
// When the repeat function is called with these inputs,
2222
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
2323

24+
test("should return the string", () => {
25+
const str = "CYF";
26+
const count = 1;
27+
const repeatedStr = repeat(str, count);
28+
expect(repeatedStr).toEqual("CYF");
29+
});
30+
2431
// case: Handle Count of 0:
2532
// Given a target string str and a count equal to 0,
2633
// When the repeat function is called with these inputs,
2734
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
2835

36+
test("should return empty when the count is 0", () => {
37+
const str = "CYF";
38+
const count = 0;
39+
const repeatedStr = repeat(str, count);
40+
expect(repeatedStr).toEqual("");
41+
});
2942
// case: Negative Count:
3043
// Given a target string str and a negative integer count,
3144
// When the repeat function is called with these inputs,
3245
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
46+
test("should return empty when the count is 0", () => {
47+
const str = "CYF";
48+
const count = -2;
49+
const repeatedStr = repeat(str, count);
50+
expect(repeatedStr).toEqual("Invalid Input must be a positive number");
51+
});

0 commit comments

Comments
 (0)