File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed
codewars/reverse-every-other-word Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ const reverse = ( str ) => {
2
+ str = str . trim ( ) . split ( ' ' ) ;
3
+ for ( let i = 1 ; i < str . length ; i += 2 ) {
4
+ str [ i ] = str [ i ] . split ( '' ) . reverse ( ) . join ( '' ) ;
5
+ }
6
+ return str . join ( ' ' ) ;
7
+ } ;
8
+
9
+ module . exports = reverse ;
Original file line number Diff line number Diff line change
1
+ const reverse = require ( './solution' ) ;
2
+
3
+ describe ( 'Reverse every other word in the string' , ( ) => {
4
+ const testCases = [
5
+ {
6
+ input : 'Did it work?' ,
7
+ output : 'Did ti work?' ,
8
+ } ,
9
+ {
10
+ input : 'I really hope it works this time...' ,
11
+ output : 'I yllaer hope ti works siht time...' ,
12
+ } ,
13
+ {
14
+ input : 'Reverse this string, please!' ,
15
+ output : 'Reverse siht string, !esaelp' ,
16
+ } ,
17
+ ] ;
18
+
19
+ it ( 'should return a string type' , ( ) => {
20
+ expect ( typeof reverse ( 'Did it work?' ) ) . toBe ( 'string' ) ;
21
+ } ) ;
22
+
23
+ it . each ( testCases ) ( 'should return the correct output' , ( { input, output } ) => {
24
+ expect ( reverse ( input ) ) . toBe ( output ) ;
25
+ } ) ;
26
+ } ) ;
You can’t perform that action at this time.
0 commit comments