File tree Expand file tree Collapse file tree 1 file changed +22
-8
lines changed
Expand file tree Collapse file tree 1 file changed +22
-8
lines changed Original file line number Diff line number Diff line change 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
119function 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+
1327module . 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
You can’t perform that action at this time.
0 commit comments