Skip to content

Commit 6a5a66c

Browse files
committed
Implement repeatStr function to re-create string repetition logic without using String.prototype.repeat
1 parent 3798b1b commit 6a5a66c

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
function repeatStr() {
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";
5-
}
4+
function repeatStr(str, count) {
5+
if (count < 0) {
6+
throw new Error("Count must be a non-negative integer");
7+
}
8+
let result = "";
9+
for (let i = 0; i < count; i++) {
10+
result += str;
11+
}
12+
return result;
13+
}
614

7-
module.exports = repeatStr;
15+
module.exports = repeatStr;
16+
}

0 commit comments

Comments
 (0)