Skip to content

Commit e09e648

Browse files
implemented a function and wrote different test cases for the repeat string function
1 parent 9a4ad71 commit e09e648

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if( count >= 0 )
3+
{return str.repeat (count)};
4+
else { return 'invalid count'};
35
}
46

57
module.exports = repeatStr;

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,29 @@ test("should repeat the string count times", () => {
2020
// Given a target string `str` and a `count` equal to 1,
2121
// When the repeatStr function is called with these inputs,
2222
// Then it should return the original `str` without repetition.
23-
23+
test("should repeat the string count times", () => {
24+
const str = "hello";
25+
const count = 1;
26+
const repeatedStr = repeatStr(str, count);
27+
expect(repeatedStr).toEqual("hello");
28+
});
2429
// Case: Handle count of 0:
2530
// Given a target string `str` and a `count` equal to 0,
2631
// When the repeatStr function is called with these inputs,
2732
// Then it should return an empty string.
28-
33+
test("should repeat the string count times", () => {
34+
const str = "hello";
35+
const count = 0;
36+
const repeatedStr = repeatStr(str, count);
37+
expect(repeatedStr).toEqual(" ");
38+
});
2939
// Case: Handle negative count:
3040
// Given a target string `str` and a negative integer `count`,
3141
// When the repeatStr function is called with these inputs,
3242
// Then it should throw an error, as negative counts are not valid.
43+
test("should repeat the string count times", () => {
44+
const str = "hello";
45+
const count = -1;
46+
const repeatedStr = repeatStr(str, count);
47+
expect(repeatedStr).toEqual("invalid count");
48+
});

0 commit comments

Comments
 (0)