Skip to content

Commit e04af18

Browse files
committed
refactor: simplify repeatStr function using built-in string method
1 parent b00a6cb commit e04af18

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1+
// function repeatStr(str, count) {
2+
// if (count < 0) {
3+
// throw new Error("count cannot be negative");
4+
// }
5+
6+
// let result = "";
7+
// for (let i = 0; i < count; i++) {
8+
// result = result + str;
9+
// }
10+
// return result;
11+
// }
12+
13+
// The repeat() method of String values constructs and returns
14+
// a new string which contains the specified number of copies
15+
// of this string, concatenated together.
16+
//repeatStr("abc", 3); // "abcabcabc"
17+
18+
//refactored function using built-in method
119
function repeatStr(str, count) {
220
if (count < 0) {
3-
throw new Error("count cannot be negative 😭");
21+
throw new Error("count cannot be negative");
422
}
5-
6-
let result = "";
7-
for (let i = 0; i < count; i++) {
8-
result = result + str;
9-
}
10-
return result;
23+
return str.repeat(count);
1124
}
1225

26+
1327
module.exports = repeatStr;
1428

1529

@@ -23,7 +37,7 @@ test("Should log the specific error message to the console", () => {
2337
console.log("Verified Error Message ->", error.message);
2438

2539
// Check if the message is exactly what we defined
26-
expect(error.message).toBe("count cannot be negative 😭");
40+
expect(error.message).toBe("count cannot be negative");
2741
}
2842

2943
// Also verify that it actually throws using Jest's built-in matcher

0 commit comments

Comments
 (0)