Skip to content

Commit 6c50af1

Browse files
committed
Use built-in .repeat() method instead of manual implementation; add one more test and verify all tests with the new function
1 parent 7172fd2 commit 6c50af1

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
function repeat(myString, repeatNumber) {
22
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;
3+
//if (repeatNumber < 0) throw new Error("Repeat count must be a positive number");
4+
// if I use this instead return how I can test it with jest??
5+
return myString.repeat(repeatNumber);
86
}
97

108
module.exports = repeat;
9+
10+
// Note:
11+
// When I wrote this code, I didn’t know there was already a built-in method for this.
12+
// Please review this implementation as well.
13+
//if (repeatNumber < 0) throw new Error("Repeat count must be a positive number"); if I use this instead return how I can test it with jest??
14+
15+
// function repeat(myString, repeatNumber) {
16+
// if (repeatNumber < 0) return "Invalid Input must be a positive number";
17+
// let str = "";
18+
// for (let i = 0; i < repeatNumber; i++) {
19+
// str += myString;
20+
// }
21+
// return str;
22+
// }
23+
24+
//if (repeatNumber < 0) throw new Error("Repeat count must be a positive number");

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,21 @@ test("should return empty when the count is 0", () => {
4343
// Given a target string str and a negative integer count,
4444
// When the repeat function is called with these inputs,
4545
// 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", () => {
46+
test("should return empty when the count is negative", () => {
4747
const str = "CYF";
4848
const count = -2;
4949
const repeatedStr = repeat(str, count);
5050
expect(repeatedStr).toEqual("Invalid Input must be a positive number");
5151
});
52+
test("should repeat the string five times when the count is 5", () => {
53+
const str = "CYF";
54+
const count = 5;
55+
const repeatedStr = repeat(str, count);
56+
expect(repeatedStr).toEqual("CYFCYFCYFCYFCYF");
57+
});
58+
test("should return an empty string when the str is empty whatever was count equal", () => {
59+
const str = "";
60+
const count = 10;
61+
const repeatedStr = repeat(str, count);
62+
expect(repeatedStr).toEqual("");
63+
});

0 commit comments

Comments
 (0)