Skip to content

Commit 1d03850

Browse files
committed
refactor repeatStr function to improve error handling and maintain structure
1 parent 6e8bd15 commit 1d03850

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

‎practice-tdd/repeat-str.js‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
export function repeatStr(word, n) {
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-
let nWords = "";
5-
for (let i = 0; i < n; i++) {
6-
nWords = nWords + word;
4+
if (n >= 0) {
5+
let nWords = "";
6+
for (let i = 0; i < n; i++) {
7+
nWords = nWords + word;
8+
}
9+
return nWords;
710
}
8-
return nWords;
11+
throw new Error("Error");
912
}

0 commit comments

Comments
 (0)