File tree Expand file tree Collapse file tree 2 files changed +32
-6
lines changed Expand file tree Collapse file tree 2 files changed +32
-6
lines changed Original file line number Diff line number Diff line change 11function repeat ( myString , repeatNumber ) {
22 if ( repeatNumber < 0 ) return "Invalid Input must be a positive number" ;
3- let str = "" ;
4- for ( let i = 0 ; i < repeatNumber ; i ++ ) {
5- str += myString ;
6- }
7- return str ;
3+ //if (repeatNumber < 0) throw new Error("Repeat count must be a positive number");
4+ // if I use this instead return how I can test it with jest??
5+ return myString . repeat ( repeatNumber ) ;
86}
97
108module . exports = repeat ;
9+
10+ // Note:
11+ // When I wrote this code, I didn’t know there was already a built-in method for this.
12+ // Please review this implementation as well.
13+ //if (repeatNumber < 0) throw new Error("Repeat count must be a positive number"); if I use this instead return how I can test it with jest??
14+
15+ // function repeat(myString, repeatNumber) {
16+ // if (repeatNumber < 0) return "Invalid Input must be a positive number";
17+ // let str = "";
18+ // for (let i = 0; i < repeatNumber; i++) {
19+ // str += myString;
20+ // }
21+ // return str;
22+ // }
23+
24+ //if (repeatNumber < 0) throw new Error("Repeat count must be a positive number");
Original file line number Diff line number Diff line change @@ -43,9 +43,21 @@ test("should return empty when the count is 0", () => {
4343// Given a target string str and a negative integer count,
4444// When the repeat function is called with these inputs,
4545// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
46- test ( "should return empty when the count is 0 " , ( ) => {
46+ test ( "should return empty when the count is negative " , ( ) => {
4747 const str = "CYF" ;
4848 const count = - 2 ;
4949 const repeatedStr = repeat ( str , count ) ;
5050 expect ( repeatedStr ) . toEqual ( "Invalid Input must be a positive number" ) ;
5151} ) ;
52+ test ( "should repeat the string five times when the count is 5" , ( ) => {
53+ const str = "CYF" ;
54+ const count = 5 ;
55+ const repeatedStr = repeat ( str , count ) ;
56+ expect ( repeatedStr ) . toEqual ( "CYFCYFCYFCYFCYF" ) ;
57+ } ) ;
58+ test ( "should return an empty string when the str is empty whatever was count equal" , ( ) => {
59+ const str = "" ;
60+ const count = 10 ;
61+ const repeatedStr = repeat ( str , count ) ;
62+ expect ( repeatedStr ) . toEqual ( "" ) ;
63+ } ) ;
You can’t perform that action at this time.
0 commit comments