Skip to content

Commit e305b05

Browse files
authored
Implement repeatStr function without String.prototype.repeat
Implement repeatStr function to repeat a string a specified number of times, handling negative counts with an error.
1 parent cad27c0 commit e305b05

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
function repeatStr() {
2-
// 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).
3-
// The goal is to re-implement that function, not to use it.
4-
return "hellohellohello";
1+
2+
function repeatStr(str, count) {
3+
if (count < 0) {
4+
throw new Error("Count cannot be negative");
5+
}
6+
7+
let result = "";
8+
9+
for (let i = 0; i < count; i++) {
10+
result += str;
11+
}
12+
13+
return result;
514
}
615

716
module.exports = repeatStr;

0 commit comments

Comments
 (0)