This repository was archived by the owner on Feb 20, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +49
-0
lines changed Expand file tree Collapse file tree 3 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -54,6 +54,7 @@ import median from './array-median'
5454import timeDifference from './timeDifference'
5555import isPrime from './is-prime'
5656import swapElements from './swapElements'
57+ import reverse from './reverse'
5758
5859export {
5960 isOdd ,
@@ -112,4 +113,5 @@ export {
112113 timeDifference ,
113114 isPrime ,
114115 swapElements ,
116+ reverse ,
115117}
Original file line number Diff line number Diff line change 1+ export default reverse
2+
3+ /**
4+ * Original Source: https://stackoverflow.com/a/4859258/7221168
5+ *
6+ * Function that recursively reverses a string
7+ *
8+ * @param {string } str - A string
9+ * @return {string } - A reversed string
10+ */
11+ function reverse ( str ) {
12+ if ( str === '' ) {
13+ return str
14+ } else {
15+ return reverse ( str . substr ( 1 ) ) + str . charAt ( 0 )
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ import test from 'ava'
2+ import { reverse } from '../src'
3+
4+ test ( 'string with only letters' , t => {
5+ const str = 'abcdefG'
6+ const expected = 'Gfedcba'
7+ const result = reverse ( str )
8+ t . is ( expected , result )
9+ } )
10+
11+ test ( 'string with only numbers' , t => {
12+ const str = '1234567890'
13+ const expected = '0987654321'
14+ const result = reverse ( str )
15+ t . is ( expected , result )
16+ } )
17+
18+ test ( 'string with letters and special characters' , t => {
19+ const str = 'abc!"§…'
20+ const expected = '…§"!cba'
21+ const result = reverse ( str )
22+ t . is ( expected , result )
23+ } )
24+
25+ test ( 'string with only special characters' , t => {
26+ const str = 'œ∑´®†'
27+ const expected = '†®´∑œ'
28+ const result = reverse ( str )
29+ t . is ( expected , result )
30+ } )
You can’t perform that action at this time.
0 commit comments