Skip to content

Commit ad57a02

Browse files
committed
repeat string file update
1 parent df39962 commit ad57a02

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
function repeatStr() {
1+
function repeatStr(str, count) {
2+
let result = "";
3+
for (let i =0; i<count;i++){
4+
result +=str;
5+
}
6+
27
// Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat).
38
// The goal is to re-implement that function, not to use it.
4-
return "hellohellohello";
9+
return result;
510
}
11+
console.log(repeatStr("hello",3))
612

713
module.exports = repeatStr;

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,35 @@ 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+
// Case: handle count of 1:
24+
test("should return the original string unchanged when count is 1", () => {
25+
const str = "apple";
26+
const count = 1;
27+
const repeatedStr = repeatStr(str, count);
28+
expect(repeatedStr).toEqual("apple");
29+
});
30+
2331

2432
// Case: Handle count of 0:
2533
// Given a target string `str` and a `count` equal to 0,
2634
// When the repeatStr function is called with these inputs,
2735
// Then it should return an empty string.
2836

37+
test("should return an empty string when count is 0", () => {
38+
const str = "world";
39+
const count = 0;
40+
const repeatedStr = repeatStr(str, count);
41+
expect(repeatedStr).toEqual("");
42+
});
43+
2944
// Case: Handle negative count:
3045
// Given a target string `str` and a negative integer `count`,
3146
// When the repeatStr function is called with these inputs,
3247
// Then it should throw an error, as negative counts are not valid.
48+
49+
test("should throw an error when count is a negative integer", () => {
50+
const str = "test";
51+
const count = -5;
52+
expect(() => repeatStr(str, count)).toThrow();
53+
});
54+

0 commit comments

Comments
 (0)