Skip to content

Commit 83c98dc

Browse files
committed
added tests for the given scenarios and created a repeat function that takes in a string and count to create a new string
1 parent 1a6131d commit 83c98dc

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
function repeat() {
2-
return "hellohellohello";
1+
function repeat(str, count) {
2+
let newStr = "";
3+
4+
if (count === 0) {
5+
return newStr;
6+
} else if (count === 1) {
7+
return (newStr += str);
8+
} else if (count > 1) {
9+
for (let i = 0; i < count; i++) {
10+
newStr += str;
11+
}
12+
return newStr;
13+
} else {
14+
return "Error invalid count used, please use integers from 0 upwards.";
15+
}
316
}
417

518
module.exports = repeat;

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,33 @@ test("should repeat the string count times", () => {
2020
// Given a target string str and a count equal to 1,
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.
23+
test("should return the original string without repetition", () => {
24+
const str = "hello";
25+
const count = 1;
26+
const repeatedStr = repeat(str, count);
27+
expect(repeatedStr).toEqual("hello");
28+
});
2329

2430
// case: Handle Count of 0:
2531
// Given a target string str and a count equal to 0,
2632
// When the repeat function is called with these inputs,
2733
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
34+
test("should return an empty string", () => {
35+
const str = "hello";
36+
const count = 0;
37+
const repeatedStr = repeat(str, count);
38+
expect(repeatedStr).toEqual("");
39+
});
2840

2941
// case: Negative Count:
3042
// Given a target string str and a negative integer count,
3143
// When the repeat function is called with these inputs,
3244
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
45+
test("should throw an error for invalid count", () => {
46+
const str = "hello";
47+
const count = -4;
48+
const repeatedStr = repeat(str, count);
49+
expect(repeatedStr).toEqual(
50+
"Error invalid count used, please use integers from 0 upwards."
51+
);
52+
});

0 commit comments

Comments
 (0)