Skip to content

Commit b09054b

Browse files
author
Ogbemi mene
committed
repeated-str
1 parent f4337c9 commit b09054b

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
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+
// Guard clause for invalid counts
3+
if (count < 0) {
4+
throw new RangeError("repeatStr count must be non-negative");
5+
}
6+
7+
let result = "";
8+
9+
// Append the string to the result string 'count' times
10+
for (let i = 0; i < count; i++) {
11+
result += str;
12+
}
13+
14+
return result;
515
}
616

7-
module.exports = repeatStr;
17+
module.exports = repeatStr;

0 commit comments

Comments
 (0)