Skip to content

Commit 1a46ec8

Browse files
author
Ogbemi mene
committed
commit for repeat
1 parent 0e7fe70 commit 1a46ec8

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
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+
function repeatStr(str, count) {
2+
// Native String.prototype.repeat throws a RangeError for negative counts
3+
if (count < 0) {
4+
throw new RangeError("Invalid count value");
5+
}
6+
7+
let result = "";
8+
for (let i = 0; i < count; i++) {
9+
result += str;
10+
}
11+
12+
return result;
513
}
614

715
module.exports = repeatStr;

0 commit comments

Comments
 (0)