Skip to content

Commit 4c0130e

Browse files
committed
refactor: simplify repeatStr function by removing redundant check for count of 1
1 parent 11a1b98 commit 4c0130e

File tree

2 files changed

+1
-17
lines changed

2 files changed

+1
-17
lines changed

Sprint-3/2-practice-tdd/repeat-str.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ function repeatStr(str, count) {
22
if (count < 0) {
33
throw new Error("count cannot be negative");
44
}
5-
if (count === 1) return str;
6-
5+
76
let result = "";
87
for (let i = 0; i < count; i++) {
98
result = result + str;

Sprint-3/2-practice-tdd/repeat-str.test.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,3 @@ test("Should return an empty string if the count is 0", () => {
4242
test("Should throw an error if the count is negative", () => {
4343
expect(() => repeatStr("hi", -3)).toThrow();
4444
});
45-
46-
function repeatStr(str, count) {
47-
if (count < 0) {
48-
throw new Error("count cannot be negative");
49-
}
50-
if (count === 1) return str;
51-
52-
let result = "";
53-
for (let i = 0; i < count; i++) {
54-
result = result + str;
55-
}
56-
return result;
57-
}
58-
59-
module.exports = repeatStr;

0 commit comments

Comments
 (0)