File tree Expand file tree Collapse file tree 2 files changed +40
-4
lines changed
Expand file tree Collapse file tree 2 files changed +40
-4
lines changed Original file line number Diff line number Diff line change 1- function repeatStr ( ) {
2- return "hellohellohello" ;
1+ function repeatStr ( str , count ) {
2+ if ( count < 0 )
3+ {
4+ throw new Error ( "negative counts are not valid" ) ;
5+ }
6+ else if ( count === 1 )
7+ {
8+ return str ;
9+ }
10+ else if ( count === 0 )
11+ {
12+ return "" ;
13+ }
14+ else
15+ {
16+ let result = "" ;
17+ for ( let i = 0 ; i < count ; i ++ )
18+ {
19+
20+ result = result + str ;
21+ }
22+ return result ;
23+ }
324}
425
526module . exports = repeatStr ;
Original file line number Diff line number Diff line change @@ -12,8 +12,23 @@ const repeatStr = require("./repeat-str");
1212test ( "should repeat the string count times" , ( ) => {
1313 const str = "hello" ;
1414 const count = 3 ;
15- const repeatedStr = repeatStr ( str , count ) ;
16- expect ( repeatedStr ) . toEqual ( "hellohellohello" ) ;
15+ expect ( repeatStr ( str , count ) ) . toEqual ( "hellohellohello" ) ;
16+ } ) ;
17+ test ( " should return original 'str' without repetition" , ( ) => {
18+ const str = "hello" ;
19+ const count = 1 ;
20+ expect ( repeatStr ( str , count ) ) . toEqual ( "hello" ) ;
21+ } ) ;
22+ test ( "should return empty string" , ( ) => {
23+ const str = "hello" ;
24+ const count = 0 ;
25+ expect ( repeatStr ( str , count ) ) . toEqual ( "" ) ;
26+
27+ } ) ;
28+ test ( "should throw negative counts are not valid Error" , ( ) => {
29+ const str = "hello" ;
30+ const count = - 1 ;
31+ expect ( ( ) => repeatStr ( str , count ) ) . toThrow ( ) ;
1732} ) ;
1833
1934// Case: handle count of 1:
You can’t perform that action at this time.
0 commit comments