Skip to content

Commit 555d87e

Browse files
committed
repeat function written and tested
1 parent 75229ec commit 555d87e

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
function repeat() {
2-
return "hellohellohello";
1+
function repeat(str, count) {
2+
if (!Number.isInteger(count) || count < 0) {
3+
throw Error("Invalid count value");
4+
}
5+
return str.repeat(count);
36
}
47

58
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
@@ -1,5 +1,6 @@
11
// Implement a function repeat
22
const repeat = require("./repeat");
3+
34
// Given a target string str and a positive integer count,
45
// When the repeat function is called with these inputs,
56
// Then it should:
@@ -20,13 +21,32 @@ test("should repeat the string count times", () => {
2021
// Given a target string str and a count equal to 1,
2122
// When the repeat function is called with these inputs,
2223
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
24+
test("should return the original string when count is 1", () => {
25+
const str = "hello";
26+
const count = 1;
27+
const repeatedStr = repeat(str, count);
28+
expect(repeatedStr).toEqual("hello");
29+
});
2330

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.
35+
test("should return an empty string when count is 0", () => {
36+
const str = "hello";
37+
const count = 0;
38+
const repeatedStr = repeat(str, count);
39+
expect(repeatedStr).toEqual("");
40+
});
2841

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 throw an error for negative count", () => {
47+
const str = "hello";
48+
const count = -2;
49+
expect(() => {
50+
repeat(str, count);
51+
}).toThrow("Invalid count value");
52+
});

0 commit comments

Comments
 (0)