File tree Expand file tree Collapse file tree 1 file changed +42
-1
lines changed
Expand file tree Collapse file tree 1 file changed +42
-1
lines changed Original file line number Diff line number Diff line change 22// "Welcome to this Javascript Guide!" should be become "emocleW ot siht tpircsavaJ !ediuG"
33
44function reverseBySeparator ( string , separator ) {
5- return string . split ( separator ) . reverse ( ) . join ( separator ) ;
5+ return string . split ( separator )
6+ . map ( word => word . split ( '' ) . reverse ( ) . join ( '' ) )
7+ . join ( separator ) ;
68}
79
810// To test
@@ -14,3 +16,42 @@ function reverseBySeparator(string, separator) {
1416//
1517// // Output becomes emocleW ot siht tpircsavaJ
1618// let reverseWord = reverseBySeparator(reverseBySentence, " ");
19+
20+ // Without using split and reverse functions
21+
22+ const reverseEachWord = str => {
23+ let words = split ( str ) ;
24+ let result = '' ;
25+
26+ for ( let word of words ) {
27+ result = result + reverse ( word ) + ' ' ;
28+ }
29+
30+ return result . trim ( ) ;
31+ }
32+
33+ const split = str => {
34+ const words = [ ] ;
35+ let word = '' ;
36+
37+ for ( let i = 0 ; i < str . length ; i ++ ) {
38+ if ( str . charAt ( i ) === ' ' ) {
39+ words . push ( word ) ;
40+ word = '' ;
41+ } else {
42+ word += str . charAt ( i ) ;
43+ }
44+ }
45+ words . push ( word ) ;
46+ return words ;
47+ }
48+
49+ const reverse = word => {
50+ let result = '' ;
51+
52+ for ( let i = word . length - 1 ; i >= 0 ; i -- ) {
53+ result += word [ i ] ;
54+ }
55+
56+ return result ;
57+ }
You can’t perform that action at this time.
0 commit comments