Skip to content

Commit dbbe47d

Browse files
committed
implement repeat string function
1 parent 94768d9 commit dbbe47d

1 file changed

Lines changed: 39 additions & 2 deletions

File tree

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,44 @@
1-
function repeatStr() {
1+
function repeatStr(str, count) {
22
// 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).
33
// The goal is to re-implement that function, not to use it.
4-
return "hellohellohello";
4+
5+
//handle negative count
6+
if (count < 0) {
7+
throw new Error("Count cannot be negative");
8+
}
9+
10+
//with valid count input
11+
let output = "";
12+
13+
for (let i = 0; i < count; i++) {
14+
output = output + str;
15+
}
16+
17+
return output;
518
}
619

720
module.exports = repeatStr;
21+
22+
// Given a target string `str` and a positive integer `count`,
23+
// When the repeatStr function is called with these inputs,
24+
// Then it should:
25+
26+
// Case: handle multiple repetitions:
27+
// Given a target string `str` and a positive integer `count` greater than 1,
28+
// When the repeatStr function is called with these inputs,
29+
// Then it should return a string that contains the original `str` repeated `count` times.
30+
31+
// Case: handle count of 1:
32+
// Given a target string `str` and a `count` equal to 1,
33+
// When the repeatStr function is called with these inputs,
34+
// Then it should return the original `str` without repetition.
35+
36+
// Case: Handle count of 0:
37+
// Given a target string `str` and a `count` equal to 0,
38+
// When the repeatStr function is called with these inputs,
39+
// Then it should return an empty string.
40+
41+
// Case: Handle negative count:
42+
// Given a target string `str` and a negative integer `count`,
43+
// When the repeatStr function is called with these inputs,
44+
// Then it should throw an error, as negative counts are not valid.

0 commit comments

Comments
 (0)