Skip to content

Commit 822c364

Browse files
committed
rewrite code
1 parent bd77971 commit 822c364

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
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, num) {
2+
var result = "";
3+
var i;
4+
if (num < 0) {
5+
throw new Error("negative numbers are not valid");
6+
} else if (num === 0) {
7+
result = "";
8+
} else {
9+
for (i = 0; i < num; i++) {
10+
result += str;
11+
}
12+
}
13+
return result;
514
}
615

7-
module.exports = repeatStr;
16+
//console.log(repeatStr("hello", -1));
17+
//console.log(repeatStr("hello", 0));
18+
//console.log(repeatStr("hello", 5))
19+
20+
module.exports = repeatStr;

0 commit comments

Comments
 (0)