Skip to content

Commit 4aaed33

Browse files
Add comprehensive tests for repeatStr function
1 parent 68928d6 commit 4aaed33

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count <0)
3+
{
4+
throw new Error("negative counts are not valid");
5+
}
6+
else if(count === 1)
7+
{
8+
return str;
9+
}
10+
else if (count === 0)
11+
{
12+
return "";
13+
}
14+
else
15+
{
16+
let result ="";
17+
for (let i =0; i<count;i++)
18+
{
19+
20+
result =result+str;
21+
}
22+
return result;
23+
}
324
}
425

526
module.exports = repeatStr;

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,23 @@ const repeatStr = require("./repeat-str");
1212
test("should repeat the string count times", () => {
1313
const str = "hello";
1414
const count = 3;
15-
const repeatedStr = repeatStr(str, count);
16-
expect(repeatedStr).toEqual("hellohellohello");
15+
expect(repeatStr(str,count)).toEqual("hellohellohello");
16+
});
17+
test(" should return original 'str' without repetition", ()=>{
18+
const str = "hello";
19+
const count = 1;
20+
expect(repeatStr(str, count)).toEqual("hello");
21+
});
22+
test("should return empty string", ()=>{
23+
const str = "hello";
24+
const count = 0;
25+
expect(repeatStr(str, count)).toEqual("");
26+
27+
});
28+
test("should throw negative counts are not valid Error", ()=>{
29+
const str = "hello";
30+
const count = -1;
31+
expect(()=> repeatStr(str, count)).toThrow();
1732
});
1833

1934
// Case: handle count of 1:

0 commit comments

Comments
 (0)